gpt4 book ai didi

sql-server - SQL select join 帮助

转载 作者:行者123 更新时间:2023-12-05 00:54:22 27 4
gpt4 key购买 nike

我在为这种情况找出查询时遇到问题。我想在此查询中使用两个表,它们是这样的:

Units
ID
Other Data
People
ID
UnitID <-- fk to Units
Other Data

这就是我想要做的:
我想选择所有在 People 表中没有一行链接到它们的单位。我该怎么做?

最佳答案

SELECT Units.* FROM Units LEFT JOIN People ON People.UnitID = Units.ID WHERE People.ID IS NULL

或者替代地

SELECT Units.* FROM Units WHERE NOT EXISTS (SELECT 1 FROM People WHERE People.UnitID = Units.ID)

甚至

SELECT Units.* FROM Units WHERE Units.ID NOT IN (SELECT UnitID FROM People)

关于sql-server - SQL select join 帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/870268/

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