当前位置:网站首页>部分 力扣 LeetCode 中的SQL刷题整理
部分 力扣 LeetCode 中的SQL刷题整理
2022-06-11 00:16:00 【cyc头发还挺多的】
1873. 计算特殊奖金
select employee_id,
case
when employee_id in (select employee_id from Employees where name not like "M%" and employee_id%2=1) then salary
else 0
end as bonus
from Employees
order by employee_id;练习 case when...then... when...then... else... end 判断语句,其结果存于end后紧跟的字段中。
196. 删除重复的电子邮箱
DELETE p1 from Person p1 join Person p2 on p1.email=p2.email and p1.id>p2.id;题目要求使用delete语句,可以先用select做。
输入
Person 表:
+---+-------+
| id | email |
+---+-------+
| 1 | A |
| 2 | B |
| 3 | A |
+---+-------+
先通过select查看所有结果
select * from Person p1 join Person p2 on p1.email=p2.email;结果为:(从左往右字段依次为:p1.id,p1.email,p2.id,p2.email)
1 A 1 A
3 A 1 A
2 B 2 B
1 A 3 A
3 A 3 A
按照题目要求,想办法删除3 A即可,
若是delete p1(只看前两个字段)则添加条件筛选出3 A 1 A或者3 A 3 A,
若是delete p2(只看后两个字段)则添加条件筛选出1 A 3 A或者3 A 3 A,
如果要筛选出3 A 3 A,只能用p1.id=p2.id,此时1 A 1 A和2 B 2 B也会被筛选出,不符合题意,
因此对于delete p1只能用p1.id>p2.id,对于delete p1只能用p1.id<p2.id。
1484. 按日期分组销售产品
select
sell_date,
count(distinct product) num_sold,
group_concat(distinct product order by product asc separator ',') products
from
Activities
group by
sell_date;group_concat:将组内的数据连接
distinct:默认按字段升序排列,若要更改排序通过order by修改
order by product asc separator ',':distinct默认升序,group_concat默认‘,’连接,因此这段sql可以省略
边栏推荐
- HandlerMethodArgumentResolver(参数解析器)的作用+使用小案例
- BlindWaterMar报错No module named cv2
- Bubble sort and quick sort
- 中国专利奖奖金多少,补贴100万
- Beijing Yanqing District high tech enterprise cultivation support standard, with a subsidy of 100000 yuan
- CentOS实战部署redis
- Array simulation [queue] and [ring queue]_ code implementation
- async await
- 对多线程的理解
- [introduction to ROS] - 03 basic concepts and instructions of ROS
猜你喜欢
[ROS tutorial] - 02 ROS installation

明朝的那些皇帝

Le meilleur outil de tambour créatif: Groove agent 5

条码固定资产管理系统的作用,固定资产条码化管理

CentOS actual deployment redis

zabbix离线安装
![[paper reading] boostmis: boosting medical image semi supervised learning with adaptive pseudolabeling](/img/10/a60cfe830e2238de00121d7bd95ad6.png)
[paper reading] boostmis: boosting medical image semi supervised learning with adaptive pseudolabeling

Logback log framework

Google搜索为什么不能无限分页?

IRS应用发布之十六:H5 应用设计指南
随机推荐
中国专利奖政策支持介绍,补贴100万
简述自定义注解
WPF basic controls
Beijing Fangshan District high tech enterprise cultivation support standard, with a subsidy of 100000 yuan
Why can't Google search page infinite?
How much is the bonus of China Patent Award, with a subsidy of 1million yuan
记录oracle的几个参数 db_files,Cursor_sharing ,open_cursor
Locks in sqlserver
table_exists_action=append和table_exists_action=truncate
Time dependent - format, operation, comparison, conversion
中国专利奖申报流程介绍,补贴100万
NVIDIA Jetson之PWM风扇自定义控制
C language implementation setting desktop wallpaper
Josephus problem_ Unidirectional circular linked list_ code implementation
增额终身寿险产品都有哪些优势?门槛高吗?
[introduction to ROS] - 03 ROS workspace and function pack
The file "setup" does not exist. What should I do?
About the log traffic monitoring and early warning small project | standardized return of interaction with the database in flask
北京延庆区高新技术企业培育支持标准,补贴10万
IRS应用发布之十六:H5 应用设计指南