SQL 选择其他列相同的 id

发布于 2024-11-26 07:46:56 字数 299 浏览 6 评论 0原文

如果我有一张包含所有者、墙壁颜色和屋顶颜色列的属性表(假设一个所有者可以拥有多栋房屋,但房屋只能有一个所有者),我如何选择仅拥有具有相同墙壁和屋顶颜色的房屋的所有所有者?

所以如果我有

owner   wallColor  roofColor
Bob     Red        Green
Bob     Blue       Blue
Greg    Yellow     Yellow
Greg    Black      Black

它应该只返回格雷格,因为鲍勃有一座红墙和绿色屋顶的房子。

If I have a table of properties with columns owner, wallColor and roofColor (let's say one owner can own multiple houses but houses can have only one owner), how can I select all owners who only have houses that have identical wall and roof colors?

So if I had

owner   wallColor  roofColor
Bob     Red        Green
Bob     Blue       Blue
Greg    Yellow     Yellow
Greg    Black      Black

it should only return Greg, because Bob has a house with red walls and a green roof.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

诠释孤独 2024-12-03 07:46:56
 SELECT DISTINCT t1.owner
 FROM tablename t1
 WHERE t1.owner NOT IN
 (
    SELECT DISTINCT t2.owner FROM tablename t2
    WHERE t2.wallColor != t2.roofColor
 )
 SELECT DISTINCT t1.owner
 FROM tablename t1
 WHERE t1.owner NOT IN
 (
    SELECT DISTINCT t2.owner FROM tablename t2
    WHERE t2.wallColor != t2.roofColor
 )
雨后咖啡店 2024-12-03 07:46:56
SELECT DISTINCT owner 
FROM ytable 
WHERE owner NOT IN 
(
    SELECT owner 
    FROM ytable 
    WHERE wallColor <> roofColor
)
SELECT DISTINCT owner 
FROM ytable 
WHERE owner NOT IN 
(
    SELECT owner 
    FROM ytable 
    WHERE wallColor <> roofColor
)
街角卖回忆 2024-12-03 07:46:56

从...中选择不同的所有者,其中墙壁颜色 = 屋顶颜色
除了
从...中选择[不同]所有者,其中wallcolor<>屋顶颜色;

:-)

SELECT DISTINCT OWNER FROM ... WHERE wallcolor = roofcolor
EXCEPT
SELECT [DISTINCT] OWNER FROM ... WHERE wallcolor <> roofcolor;

:-)

一人独醉 2024-12-03 07:46:56
SELECT DISTINCT owner FROM table WHERE wallColor=roofColor AND 
owner NOT IN ( SELECT DISTINCT owner FROM table WHERE wallColor<>roofColor )

这将是我的方法,但可能需要一些调整。

SELECT DISTINCT owner FROM table WHERE wallColor=roofColor AND 
owner NOT IN ( SELECT DISTINCT owner FROM table WHERE wallColor<>roofColor )

That would be my approach, but it may need some tweaking.

兮颜 2024-12-03 07:46:56

使用 exists 运算符:

select *
from table t
where not exists(
    select 1
    from table t1
    where t1.owner = t.owner and t1.wallColor <> t1.roofColor
)

Using exists operator:

select *
from table t
where not exists(
    select 1
    from table t1
    where t1.owner = t.owner and t1.wallColor <> t1.roofColor
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文