首页 > 开发 > 综合 > 正文

183. Customers Who Never Order

2024-07-21 02:52:32
字体:
来源:转载
供稿:网友

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;


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表