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.
Table: Customers
.
+----+-------+| Id | Name |+----+-------+| 1 | Joe || 2 | Henry || 3 | Sam || 4 | Max |+----+-------+Table:
Orders
.+----+------------+| Id | CustomerId |+----+------------+| 1 | 3 || 2 | 1 |+----+------------+Using the above tables as example, return the following:
+-----------+| Customers |+-----------+| Henry || Max |+-----------+題目是想將Customers中某一類name顯示出來,這個條件通過Orders判斷。
可以通過left join將Customers與Orders連接起來,然后篩選出符合條件的。
以例題為例:
Customers left join Orders的結(jié)果:
條件是:沒有訂單的顧客的名字,表示為CustomerId is NULL;
select Customers.Name as Customersfrom Customers left join Orderson Customers.Id=Orders.CustomerIdwhere Orders.CustomerId is NULL;
新聞熱點
疑難解答
圖片精選