mysql - SQL:基于另外两列提取信息
问题描述
请耐心等待,这有点难以解释:试图找到一种方法来提取“类型”列中包含“Op”的名称,但“代码”值必须与“类型”唯一
如:(使用下面的前3行)
不要拉第一行或第二行,因为“SPX”与“Com”和“Op”都相关,但要拉第三行,因为“VPA”在“Type”列中只有“Op”。因此,“code”列下的值不能同时在每个帐户的 type 下的“Op”和“Com”中。
输入”
Name Type Code
John Com SPX
John Op SPX
John Op VPA
John Op SPX
Matt Op SPX
Matt Op SPX
Jane Com SPX
Jane Com SPR
Jack Op SPR
Jack Op SPX
Jack Com SPR
输出:
Name Type Code
John Op VPA
Matt Op SPX
Matt Op SPX
Jack Op SPX
非常感谢任何帮助!
谢谢!
解决方案
试试这个 NOT EXISTS
select *
from
mytable t1
where not exists
(
select 1 from mytable t2
where t2.type = 'com'
and t2.name = t1.name -- if the name exists with a com type then exclude
)