★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9720985.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。

Customers 表:

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

Orders 表:

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

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

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

方法:使用子查询和NOT IN子句

算法:如果我们有一个订购过的客户列表,很容易知道谁从未订购过。

我们可以使用以下代码来获取此类列表。

select customerid from orders;

然后,我们可以NOT IN用来查询不在此列表中的客户。

1 select customers.name as 'Customers'
2 from customers
3 where customers.id not in
4 (
5     select customerid from orders
6 );


222ms

1 # Write your MySQL query statement below
2 SELECT Name as 'Customers' from Customers where Id not in (select CustomerId from Orders)

转载于:https://www.cnblogs.com/strengthen/p/9720985.html

[SQL]LeetCode183. 从不订购的客户 | Customers Who Never Order相关推荐

  1. LeetCode-183. 从不订购的客户( Customers Who Never Order)。

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

  2. leetcode183. 从不订购的客户(SQL)

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

  3. 2022-12-01:从不订购的客户。找出所有从不订购任何东西的客户,以下数据的答案输出是Henry和Max,sql语句如何写? DROP TABLE IF EXISTS `customers`; C

    2022-12-01:从不订购的客户.找出所有从不订购任何东西的客户,以下数据的答案输出是Henry和Max,sql语句如何写? DROP TABLE IF EXISTS `customers`; C ...

  4. 「力扣数据库」183. 从不订购的客户(第五天)

    183. 从不订购的客户 题目 解题 题目 题目难度:简单 某网站包含两个表,Customers 表和 Orders 表.编写一个 SQL 查询,找出所有从不订购任何东西的客户. Id Name 1 ...

  5. 182. 查找重复的电子邮箱 183. 从不订购的客户

    文章目录 一.182. 查找重复的电子邮箱 二.解决方法 1.GROUP BY+HAVING 2.GROUP BY+子查询 三.183. 从不订购的客户 一.182. 查找重复的电子邮箱 编写一个 S ...

  6. LeetCode(183)—— 从不订购的客户(MySQL)

    某网站包含两个表,Customers 表和 Orders 表.编写一个 SQL 查询,找出所有从不订购任何东西的客户. Customers 表: Id Name 1 Joe 2 Henry 3 Sam ...

  7. 数据库题目 - 从不订购的客户

    文章目录 1. 题目描述 2.解答 1. 题目描述 某网站包含两个表,Customers 表和 Orders 表.编写一个 SQL 查询,找出所有从不订购任何东西的客户. Customers 表: ± ...

  8. LetCode-MSSQL从不订购的客户

    解法(1):思路为先查询出订购的客户再使用not in查询出不包含订购客户的其他人也就是从来不订购的客户 查询出订购的客户语句: select a.Id from Customers as a,Ord ...

  9. 文巾解题 183. 从不订购的客户

    1 题目描述 2 解题思路 子查询找订了东西的用户 SELECT Name AS Customers FROM Customers WHERE ID NOT IN(SELECT CustomerIdF ...

最新文章

  1. 使用Python批量删除windows下特定目录的N天前的旧文件实战:Windows下批量删除旧文件、清除缓存文件、解救C盘、拒绝C盘爆炸
  2. typora最好用的主题_谁是我心中最好的写作工具?
  3. Spring 4 CustomEditorConfigurer Example--转
  4. C 指针总结
  5. 服务器端会话技术Session|| Session的原理||Session的细节||session的钝化session的活化||session的特点||session与Cookie的区别
  6. 滴滴开源Android插件化框架VirtualAPK原理分析
  7. MyBatis下MySqL用户口令不能为空
  8. python学习-图片处理小程序(PIL的安装、PIL.Image的使用、 示例程序(图片灰度值修改))
  9. IDE 插件新版本发布,开发效率 “biu” 起来了
  10. 严格匹配_2020湖北省考招录“刚柔并济”强调“人岗匹配”
  11. APPCAN学习笔记005---AppCan IDE安装下载,AppCan IDE启动
  12. 简单介绍一下树莓派zero w
  13. 使用VBA实现Excel下拉多选
  14. 秃头不用怕!程序员脱发自救指南来了
  15. SpringBoot整合jersey
  16. 用手机怎么看服务器里的文件,手机查看云服务器文件
  17. MIX2 适配看这一篇就够了,18:9 只需一行代码
  18. redis 关闭保护模式
  19. python设计迷宫_用Python制作迷宫GIF
  20. 来点基础的东西,关于浮点数的大小端转换以及浮点数的格式解析

热门文章

  1. 第一章-从双向链表学习设计
  2. hibernate Criteria(条件查询接口)
  3. 开始使用gradle
  4. Redis源码解析:21sentinel(二)定期发送消息、检测主观下线
  5. U-boot 打补丁,编译,设置环境变量,
  6. SQL 连接字符串的说明(转)
  7. 网页获取客户端IP的两种方法
  8. spring boot mybatis拦截器
  9. 机器学习理论梳理2 : KNN K近邻分类模型
  10. [poj 1364]King[差分约束详解(续篇)][超级源点][SPFA][Bellman-Ford]