当前位置: 首页 > article >正文

SQL-leetcode-183. 从不订购的客户

183. 从不订购的客户

Customers 表:

±------------±--------+
| Column Name | Type |
±------------±--------+
| id | int |
| name | varchar |
±------------±--------+
在 SQL 中,id 是该表的主键。
该表的每一行都表示客户的 ID 和名称。
Orders 表:

±------------±-----+
| Column Name | Type |
±------------±-----+
| id | int |
| customerId | int |
±------------±-----+
在 SQL 中,id 是该表的主键。
customerId 是 Customers 表中 ID 的外键( Pandas 中的连接键)。
该表的每一行都表示订单的 ID 和订购该订单的客户的 ID。

找出所有从不点任何东西的顾客。

以 任意顺序 返回结果表。

结果格式如下所示。

示例 1:

输入:
Customers table:
±—±------+
| id | name |
±—±------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
±—±------+
Orders table:
±—±-----------+
| id | customerId |
±—±-----------+
| 1 | 3 |
| 2 | 1 |
±—±-----------+
输出:
±----------+
| Customers |
±----------+
| Henry |
| Max |
±----------+

题解

  • 该表的每一行都表示订单的 ID 和订购该订单的客户的 ID。
    啥子含义?就是明细表呗,主键唯一,客户唯一,订单唯一,客户与订单是1对n的关系,也就是说订单是最完整的数据,客户可能没有订单,但有了订单已经能找到对应的客户。
  • 找出所有从不点任何东西的顾客。
    从来不点任何东西,啥子含义?说白了就是在订单明细表中不存在任何一条记录的客户。
    那这样是不是就分析清晰了?怎么判断这个表里的数据有没有在另外一个表里呢? join拉齐? union很显然不合适
    还要判断不在某一个表中的数据呢?外连接比较合适,关联不上会置空嘛
    于是乎方法一诞生

方法一 外连接+where

select 
    c1.name as customers
from customers c1 left join orders o1 on c1.id=o1.customerId
where o1.id is null

方法二 子查询

什么思路呢?先找到点单的有哪些,再逆向思维取反,搞定

select 
    name as customers
from customers 
where id not in(select distinct customerId from orders)

方法三 不用in还有什么吗?not exists

思路同上,找到另外一张表不存在的记录,但exists 会比子查询性能更佳
not exists (select distinct customerId from orders o where c.id=o.customerId)
这是整体,exists 返回值是 true或者false,可能会有人看不懂,简单解释下

select 
    name as customers
from customers c
where not exists (select distinct customerId from orders o where c.id=o.customerId)

这里贴个图就不多bb拉
在这里插入图片描述

方法四 暂时没想到,等想到了再说,够用就行


http://www.kler.cn/a/467243.html

相关文章:

  • qt中如何判断字符串是否为数字,整数,浮点数?
  • 【LVGL】给SquareLineStudio导出的Arduino工程添加物理按键
  • 树莓派4b如何连接ov7670摄像头
  • pyinstaller冻结打包多进程程序的bug:无限创建进程直至系统崩溃
  • Computed在Vue2、Vue3写法的不同
  • 奇怪的Python:为何 list 和 dict 的元素顺序从 Python 3.7 开始保持插入顺序?
  • ROS小记
  • 提升汽车金融租赁系统的效率与风险管理策略探讨
  • DELL EMC Unity 存储系统扩容之传统池扩容
  • CSS clip-path 属性
  • Vue项目整合与优化
  • 2024秋语法分析作业-B(满分25分)
  • 【0x0014】HCI_Read_Local_Name命令详解
  • 在 macOS 上,你可以使用系统自带的 终端(Terminal) 工具,通过 SSH 协议远程连接服务器
  • 每天40分玩转Django:Django Celery
  • 升级ubuntu24后遗症
  • redis7基础篇2 redis的哨兵模式2
  • BGP基础配置实验
  • 【第二部分--Python之基础】04 函数
  • 广域网连接PPP