某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。
Customers 表:
+----+-------+
| Id | Name |
+----+-------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
+----+-------+
Orders 表:
+----+------------+
| Id | CustomerId |
+----+------------+
| 1 | 3 |
| 2 | 1 |
+----+------------+
例如给定上述表格,你的查询应返回:
+-----------+
| Customers |
+-----------+
| Henry |
| Max |
+-----------+
答案:
1.
#内连接
select Name as Customers from Customers where id not in (
select Orders.CustomerId from Orders
)
2.
#连接查询(可扩展右连接)
SELECT L1.Name Customers
FROM Customers L1 LEFT JOIN Orders L2
ON L1.Id = L2.CustomerId
WHERE L2.Id IS NULL
leetcode、mysql 183. 从不订购的客户
最新推荐文章于 2024-10-31 16:16:13 发布