★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-cdgozqyc-md.html
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Suppose that a website contains two tables, the Customers
table and the Orders
table. Write a SQL query to find all customers who never order anything.git
Table: Customers
.github
+----+-------+ | Id | Name | +----+-------+ | 1 | Joe | | 2 | Henry | | 3 | Sam | | 4 | Max | +----+-------+
Table: Orders
.web
+----+------------+ | Id | CustomerId | +----+------------+ | 1 | 3 | | 2 | 1 | +----+------------+
Using the above tables as example, return the following:算法
+-----------+ | Customers | +-----------+ | Henry | | Max | +-----------+
某网站包含两个表,Customers
表和 Orders
表。编写一个 SQL 查询,找出全部从不订购任何东西的客户。微信
Customers
表:app
+----+-------+ | Id | Name | +----+-------+ | 1 | Joe | | 2 | Henry | | 3 | Sam | | 4 | Max | +----+-------+
Orders
表:网站
+----+------------+ | Id | CustomerId | +----+------------+ | 1 | 3 | | 2 | 1 | +----+------------+
例如给定上述表格,你的查询应返回:spa
+-----------+ | Customers | +-----------+ | Henry | | Max | +-----------+
NOT IN
子句算法:若是咱们有一个订购过的客户列表,很容易知道谁从未订购过。code
咱们能够使用如下代码来获取此类列表。
select customerid from orders;
而后,咱们能够NOT IN
用来查询不在此列表中的客户。
1 select customers.name as 'Customers' 2 from customers 3 where customers.id not in 4 ( 5 select customerid from orders 6 );
222ms
1 # Write your MySQL query statement below 2 SELECT Name as 'Customers' from Customers where Id not in (select CustomerId from Orders)