ITPub博客

首页 > IT职业 > IT图书 > (LeetCode:数据库)从不订购的客户

(LeetCode:数据库)从不订购的客户

IT图书 作者:胡扯 时间:2021-09-09 10:05:51 0 删除 编辑

某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。

Customers

+----+-------+| Id | Name  |
+----+-------+
| 1  | Joe   || 2  | Henry |
| 3  | Sam   || 4  | Max   |
+----+-------+

Orders

+----+------------+| Id | CustomerId |
+----+------------+
| 1  | 3          || 2  | 1          |
+----+------------+

例如给定上述表格,你的查询应返回:

+-----------+| Customers |+-----------+| Henry     || Max       |+-----------+

Solution

SELECT
    c. NAME AS customers
FROM
    customers c
WHERE
    c.id NOT IN (
        SELECT
            a.id
        FROM
            customers a,
            orders b
        WHERE
            a.id = b.customerId
    );



作者:lconcise
链接:https://www.jianshu.com/p/80b6b7517a8d



来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/166/viewspace-2816245/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-11

  • 博文量
    0
  • 访问量
    499