某网站包含两个表,Customers
表和 Orders
表。找出所有从不订购任何东西的客户.
Customers 表:
+----+-------+
| Id | Name |
+----+-------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
+----+-------+
Orders 表:
+----+------------+
| Id | CustomerId |
+----+------------+
| 1 | 3 |
| 2 | 1 |
+----+------------+
代码:
select c.name as customers
from customers c
left join orders o
on c.Id=o.customerId
where o.customerId is NULL;
联结公式:
select <tablename1>.<column_name>,<tablename2>.<column_name2>,<... >
from <tablename1>
left join <tablename2>
on <tablename1>.<column_name>=<tablename2>.<column_name2>
where <...>;