这是我的桌子的样子:
╔══════╦═══════╗
║ USER ║ COLOR ║
╠══════╬═══════╣
║ a ║ Red ║
║ b ║ Blue ║
║ c ║ Blue ║
║ b ║ Red ║
║ a ║ Red ║
║ c ║ White ║
╚══════╩═══════╝
我只需要只有颜色=“红色”的行。
它必须返回“a”(“b”也包含值“Blue”)。
如何设置选择?
您可以使用:
select *
from yourtable t1
where color = 'red'
and exists (select user
from yourtable t2
where t1.user = t2.user
group by user
having count(distinct color) = 1)
或者没有子查询,您可以使用:
select *
from yourtable
group by user
HAVING SUM(color = 'red') = COUNT(*)
尝试他的查询
Select *
from tbl
where color = 'RED' AND USER not in
(Select USER from tbl where color <> 'RED');