★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(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)