gpt4 book ai didi

mysql - 我怎样才能使用连接查询来找到交集

转载 作者:行者123 更新时间:2023-11-28 23:38:25 26 4
gpt4 key购买 nike

我想找到没有下过单的用户

我的想法是,获取所有用户 ID 并减去订单表中的唯一 ID

如何将其转换为 MySQL 查询语法。

mysql> select * from users;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)

mysql> select * from orders;
+------+---------+--------+
| id | user_id | amount |
+------+---------+--------+
| 1 | 1 | 10.00 |
| 1 | 2 | 5.00 |
+------+---------+--------+
2 rows in set (0.00 sec)

最佳答案

想法是在用户(左表)和订单表之间进行左连接。

然后您需要从这个连接表中过滤掉那些没有任何顺序的记录。所以在这种情况下 orders.id 将为 NULL。

SELECT 
users.id
FROM users
LEFT JOIN orders
ON users.id = orders.user_id
WHERE orders.id IS NULL

视觉理解:

enter image description here

更好地解释了 SQL 联接 here .

关于mysql - 我怎样才能使用连接查询来找到交集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35122925/

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