如果我有一个包含列所有者,wallColor和roofColor的属性表(假设一个所有者可以拥有多个房屋,但房屋只能拥有一个所有者),我该如何选择所有只拥有相同墙的房屋的所有者屋顶颜色?
所以,如果我有
owner wallColor roofColor
Bob Red Green
Bob Blue Blue
Greg Yellow Yellow
Greg Black Black
它应该只返回格雷格,因为鲍勃有一座红墙和绿屋顶的房子。
答案 0 :(得分:2)
SELECT DISTINCT owner
FROM ytable
WHERE owner NOT IN
(
SELECT owner
FROM ytable
WHERE wallColor <> roofColor
)
答案 1 :(得分:2)
SELECT DISTINCT t1.owner
FROM tablename t1
WHERE t1.owner NOT IN
(
SELECT DISTINCT t2.owner FROM tablename t2
WHERE t2.wallColor != t2.roofColor
)
答案 2 :(得分:1)
SELECT DISTINCT OWNER FROM ... WHERE wallcolor = roofcolor 除了 SELECT [DISTINCT] OWNER FROM ... WHERE wallcolor&lt;&gt; roofcolor;
: - )
答案 3 :(得分:0)
SELECT DISTINCT owner FROM table WHERE wallColor=roofColor AND
owner NOT IN ( SELECT DISTINCT owner FROM table WHERE wallColor<>roofColor )
那将是我的方法,但可能需要一些调整。
答案 4 :(得分:0)
使用exists
运算符:
select *
from table t
where not exists(
select 1
from table t1
where t1.owner = t.owner and t1.wallColor <> t1.roofColor
)