題目:
Table: Customers
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| name | varchar |
+-------------+---------+
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 |
+-------------+------+
id is the primary key column for this table.
customerId is a foreign key 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.
Write an SQL query to report all customers who never order anything.
Return the result table in any order.
給定兩個table,一個是消費者資訊,一個是訂單資訊
找出沒點過東西的消費者
SELECT name as Customers
FROM Customers
LEFT JOIN Orders ON Customers.id = Orders.customerId
WHERE Orders.customerId IS NULL;
將兩個table結合,條件訂消費者id和訂單消費者id相等
記得用left join這樣未達成條件的人customerId才會是NULL
之後將customerId是NULL的人選出就好
最後執行時間377ms(faster than 98.66%)
那我們下題見