[英]Select a row with just one value in a table
This is my table looks: 这是我的表看起来:
(I don't say it before, colum2 is string type, not an integer, sorry about it) (我之前没有说过,colum2是字符串类型,不是整数,对不起)
╔══════╦═══════╗
║ USER ║ COLOR ║
╠══════╬═══════╣
║ a ║ Red ║
║ b ║ Blue ║
║ c ║ Blue ║
║ b ║ Red ║
║ a ║ Red ║
║ c ║ White ║
╚══════╩═══════╝
I've just need the rows which has exclusively color= "Red". 我只需要具有专门的颜色行=“红”。
It must return "a" ("b" contains value "Blue" too). 它必须返回“a”(“b”也包含值“Blue”)。
How can I set the select? 如何设置选择? Thanks and regards
谢谢并恭祝安康
SELECT column1
FROM TableName
GROUP BY Column1
HAVING COUNT(DISTINCT column2) = 1
AND MAX(column2) = 1
here's another way, 这是另一种方式,
SELECT column1
FROM TableName
GROUP BY Column1
HAVING MAX(column2) = MIN(column2) AND
MAX(column2) = 1
UPDATE 1 更新1
SELECT user
FROM TableName
GROUP BY user
HAVING SUM(color = 'red') = COUNT(*)
The more efficient way to do this is by comparing the min
and max
values: 更有效的方法是比较
min
和max
:
select column1
from t
group by column1
having min(column2) = max(column2) and min(column2) = 1
If the column can take on NULL values, you will need to exclude those as well: 如果列可以采用NULL值,则还需要排除这些值:
select column1
from t
group by column1
having min(column2) = max(column2) and min(column2) = 1 and count(column2) = count(*)
Select distinct Column1 from Table1 Where Column2='1'
Or if it is integer then: 或者,如果它是整数,那么:
Select distinct Column1 from Table1 Where Column2=1
Assuming that Table1 is the table name 假设Table1是表名
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.