Fork me on GitHub

leetcode-sql-从不订购的客户

LeetCode-SQL-183-从不订购的客户

本文讲解的是LeetCode-SQL的第183题目,题目名为:从不订购的客户

难易程度:简单。

图解SQL连接

关于连接相关的问题,记住下面的图形:

题目

思路

个人方法1

找到订购的客户进行排除即可:使用的是子查询的结果进行排除

1
2
3
4
5
6
select
name as Customers
from Customers
where Id not in ( -- 查找出买过东西的顾客进行排除
select CustomerId from Orders
);

个人方法2

通过两个表的左连接来实现:,需要指定连接的条件

1
2
3
4
5
6
select
c.Name as Customers
from Customers c
left join Orders o
on c.Id = o.CustomerId
where o.CustomerId is Null; -- 找出o.CustomerId为空的数据

参考方法

网上参考的思路:使用谓词exists

1
2
3
4
5
select
c.Name as Customers
from Customers c
where not exists (select 1 from Orders -- 不存在相等情况下的数据
o where o.CustomerId = c.Id);

本文标题:leetcode-sql-从不订购的客户

发布时间:2021年06月08日 - 20:06

原始链接:http://www.renpeter.cn/2021/06/08/leetcode-sql-%E4%BB%8E%E4%B8%8D%E8%AE%A2%E8%B4%AD%E7%9A%84%E5%AE%A2%E6%88%B7.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Coffee or Tea