标签:HERE customers 查询 pre sam 客户 left join tom mys
1 从不订购的客户
某网站包含两个表,Customers
表和 Orders
表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。
Customers
表:
+----+-------+ | Id | Name | +----+-------+ | 1 | Joe | | 2 | Henry | | 3 | Sam | | 4 | Max | +----+-------+
Orders
表:
+----+------------+ | Id | CustomerId | +----+------------+ | 1 | 3 | | 2 | 1 | +----+------------+
例如给定上述表格,你的查询应返回:
+-----------+ | Customers | +-----------+ | Henry | | Max | +-----------+
解答① 联接
select c.name as customers from customers c left join orders o on c.id = o.customerid where customerid is null
解答② not in
select c.name as customers from customers c where c.id not in (select customerid from orders)
标签:HERE customers 查询 pre sam 客户 left join tom mys
原文地址:https://www.cnblogs.com/islvgb/p/9428650.html