8/2 每日一題(MYSQL 找出沒有訂單紀錄的顧客LEFT JOIN)
Table: Customers
+-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | name | varchar | +-------------+---------+ In SQL, id is the primary key column for this table. Each row of this table indicates the ID and name of a customer.
Table: Orders
+-------------+------+ | Column Name | Type | +-------------+------+ | id | int | | customerId | int | +-------------+------+ In SQL, id is the primary key column for this table. customerId is a foreign key (join key in Pandas) of the ID from the Customers table. Each row of this table indicates the ID of an order and the ID of the customer who ordered it.
Find all customers who never order anything.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input: Customers table: +----+-------+ | id | name | +----+-------+ | 1 | Joe | | 2 | Henry | | 3 | Sam | | 4 | Max | +----+-------+ Orders table: +----+------------+ | id | customerId | +----+------------+ | 1 | 3 | | 2 | 1 | +----+------------+ Output: +-----------+ | Customers | +-----------+ | Henry | | Max | +-----------+
# Write your MySQL query statement below
SELECT Customers.name AS Customers
#改表格別名
FROM Customers LEFT JOIN Orders
#左合併
ON Customers.id=Orders.customerId
WHERE Orders.id IS NULL;
#篩選出過濾掉Orders有對應訂單的紀錄id
標籤: leetcode

0 個意見:
張貼留言
訂閱 張貼留言 [Atom]
<< 首頁