gpt4 book ai didi

php - SQL 从表中选择不在其他表中的所有内容

转载 作者:行者123 更新时间:2023-11-29 08:04:21 25 4
gpt4 key购买 nike

我正在尝试选择其用户 ID 不在另一个表中的所有用户。

用户表(用户 ID 为 user_id)

Ghosts 表(用户 ID 为 id)

这是我到目前为止得到的:

$sql = "select * from users where users.user_id <> ghosts.id;";

请指教...

干杯!

最佳答案

您可以使用 NOT IN:

select * from users u where u.user_id not in (select id from ghosts);

或不存在:

select *
from users u
where not exists (select 1 from ghosts g where g.id = u.user_id)

关于php - SQL 从表中选择不在其他表中的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23025332/

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