大家好,我是小魔龙,Unity3D软件工程师,VR、AR,虚拟仿真方向,不定时更新软件开发技巧,生活感悟。
“编写SQL查看,找出所有从不订购任何东西的客户。”
题目链接:
来源:力扣(LeetCode)
链接: 183. 从不订购的客户 - 力扣(LeetCode)
某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。
Customers 表:
+----+-------+
| Id | Name |
+----+-------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
+----+-------+Orders 表:
+----+------------+
| Id | CustomerId |
+----+------------+
| 1 | 3 |
| 2 | 1 |
+----+------------+示例 1:
输入:
输出:
+-----------+
| Customers |
+-----------+
| Henry |
| Max |
+-----------+示例 2:这道题题意要求我们找到从不订购任何产品的客户。
首先,就可以找到曾经订购过的客户名单,这样不在这个名单中的用户就是答案了。
可以用 CustomerId 在 Orders 表查找,得到曾经订购过产品的客户。
select customerid from orders;然后使用关键字 not in 查询不在这个表的客户即可。
代码参考:
select customers.name as 'Customers'
from customers
where customers.id not in
(
select customerid from orders
);时间复杂度:
空间复杂度:
1、得到订购过产品的客户列表 2、查询不在此列表的客户