183. Customers Who Never Order

来源:互联网 发布:淘宝号不能登陆优酷 编辑:程序博客网 时间:2024/06/04 19:35

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的结果:


条件是:没有订单的顾客的名字,表示为CustomerId is NULL;


select Customers.Name as Customersfrom Customers left join Orderson Customers.Id=Orders.CustomerIdwhere Orders.CustomerId is NULL;


0 0
原创粉丝点击