gpt4 book ai didi

sql查询问题/计数

转载 作者:行者123 更新时间:2023-11-29 01:01:55 28 4
gpt4 key购买 nike

我有属于街道的房子。一个用户可以买几套房子。我如何知道用户是否拥有整条街道?

street table with columns (id/name)
house table with columns (id/street_id [foreign key]
owner table with columns (id/house_id/user_id) [join table with foreign keys]

到目前为止,我使用的是返回结果的计数:

select count(*), street_id from owner left join house on owner.house_id = house.id group by street_id where user_id = 1
count(*) | street_id
3 | 1
2 | 2

更一般的计数:

select count(*) from house group by street_id returns:
count(*) | street_id
3 | 1
3 | 2

我如何才能知道用户 1 拥有整条街道 1 而不是街道 2?

谢谢。

最佳答案

做一个反向查询,查询街上所有不是你要找的用户的业主。如果您的结果集 > 0,则表示用户不拥有整条街道。

 select count(*), street_id from owner left join house on owner.house_id = house.id group by street_id where user_id != 1

关于sql查询问题/计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2574526/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com