SQL 选择其他列相同的 id
如果我有一张包含所有者、墙壁颜色和屋顶颜色列的属性表(假设一个所有者可以拥有多栋房屋,但房屋只能有一个所有者),我如何选择仅拥有具有相同墙壁和屋顶颜色的房屋的所有所有者?
所以如果我有
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
从...中选择不同的所有者,其中墙壁颜色 = 屋顶颜色
除了
从...中选择[不同]所有者,其中wallcolor<>屋顶颜色;
:-)
SELECT DISTINCT OWNER FROM ... WHERE wallcolor = roofcolor
EXCEPT
SELECT [DISTINCT] OWNER FROM ... WHERE wallcolor <> roofcolor;
:-)
这将是我的方法,但可能需要一些调整。
That would be my approach, but it may need some tweaking.
使用
exists
运算符:Using
exists
operator: