这是我的表格:
(我之前没有说过,colum2是字符串类型,不是整数,对不起)
╔══════╦═══════╗
║ USER ║ COLOR ║
╠══════╬═══════╣
║ a ║ Red ║
║ b ║ Blue ║
║ c ║ Blue ║
║ b ║ Red ║
║ a ║ Red ║
║ c ║ White ║
╚══════╩═══════╝
我只需要独占 color =“Red”的行。
它必须返回“a”(“b”也包含值“Blue”)。
如何设置选择?谢谢和问候
答案 0 :(得分:3)
SELECT column1
FROM TableName
GROUP BY Column1
HAVING COUNT(DISTINCT column2) = 1
AND MAX(column2) = 1
这是另一种方式,
SELECT column1
FROM TableName
GROUP BY Column1
HAVING MAX(column2) = MIN(column2) AND
MAX(column2) = 1
更新1
SELECT user
FROM TableName
GROUP BY user
HAVING SUM(color = 'red') = COUNT(*)
答案 1 :(得分:0)
更有效的方法是比较min
和max
值:
select column1
from t
group by column1
having min(column2) = max(column2) and min(column2) = 1
如果列可以采用NULL值,则还需要排除这些值:
select column1
from t
group by column1
having min(column2) = max(column2) and min(column2) = 1 and count(column2) = count(*)
答案 2 :(得分:-1)
Select distinct Column1 from Table1 Where Column2='1'
如果它是整数,那么:
Select distinct Column1 from Table1 Where Column2=1
假设Table1是表名