CREATE TABLE table1(kid char(2),color varchar(9));
INSERT INTO table1('k1'.'yello');
INSERT INTO table1('k1'.'red');
INSERT INTO table1('k2'.'yello');
INSERT INTO table1('k2'.'blue');
INSERT INTO table1('k3'.'yello');
问:显示table1的kid
,其color
值为黄色和红色(两者都有)?
什么是sql查询?
答案 0 :(得分:1)
使用:
SELECT t.kid
FROM TABLE1 t
WHERE t.color IN ('yellow', 'red')
GROUP BY t.kid
HAVING COUNT(DISTINCT t.color) = 2
IN
子句只会获取color
值为黄色或红色的记录GROUP BY
是删除重复项所必需的COUNT(DISTINCT t.color) = 2
可确保返回有效的kid
值。如果没有DISTINCT
,则两个黄色/等将满足COUNT
检查答案 1 :(得分:0)
SELECT kid
FROM table1 as t,
table1 as t2
WHERE t1.color = 'yellow'
AND t1.kid = t2.kid
AND t2.color = 'red'