某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。
Customers 表:

Orders 表:

例如给定上述表格,你的查询应返回:

解法一:使用where条件把购买过的用户从客户表中排除
select name as customers from customers where id not in( select customerid from orders)
解法二:联表查询
select name as customers from customers where id not in
(select c.id from customers c ,orders o where c.id=o.customerid)
本文介绍如何使用SQL查询从不订购任何东西的客户。通过两种方法实现:一是使用where条件排除已购买用户;二是联表查询,从Customers表中筛选未出现在Orders表中的客户ID。
749

被折叠的 条评论
为什么被折叠?



