当前位置:网站首页>【LeetCode】【SQL】刷题笔记
【LeetCode】【SQL】刷题笔记
2022-07-03 18:55:00 【游戏编程】
code格式规范:所有关键字大写,关键字右边对齐,子句缩进
列名最好和表中的列名一样(大小写一样)
SELECT nameFROM customer CWHERE C.id NOT IN ( SELECT C1.id FROM customer C1 WHERE C1.referee_id = 2 ); 参考资料:
官方文档:【参考:MySQL :: MySQL 5.7 参考手册 :: 13 SQL 语句】
【参考:MySQL 中文文档 | MySQL 中文网】
【参考:SQL中的谓词 - 知乎】
题目以类型归类,记录常用且易错的类型
IS NULL
【参考:584. 寻找用户推荐人 【比官方细,千字干货!】(三值运算,NULL) - 寻找用户推荐人 - 力扣(LeetCode)】
【参考:584. 寻找用户推荐人 - 简单 - 力扣(LeetCode)】
SELECT name FROM customer WHERE referee_id != 2 OR referee_id IS NULL;多表查询
【参考:183. 从不订购的客户 - 力扣(LeetCode)】
- 子查询
select customers.name as 'Customers' # as 别名from customerswhere customers.id not in( select customerid from orders);# 先从orders中找出customeridSELECT c.Name as CustomersFROM Customers as cleft join Orders as o on c.Id=o.CustomerIdwhere o.Id IS NULLIF 表达式
IF( expr1 , expr2 , expr3 ) 【参考:1873. 计算特殊奖金 - 力扣(LeetCode)】
【参考:MySQL, 7种解法 - 计算特殊奖金 - 力扣(LeetCode)】
SELECT employee_id, IF( employee_id%2=1 and name not like 'M%', salary, 0 ) as bonus FROM EmployeesORDER by employee_id UPDATE Salary set sex=IF(sex='f','m','f')CASE WHEN END
UPDATE salarySET sex = CASE sex WHEN 'm' THEN 'f' ELSE 'm' END自连接
【参考:196. 删除重复的电子邮箱 - 力扣(LeetCode)】
【参考:对「官方」题解中 “delete” 和 “>” 的解释,推荐! - 删除重复的电子邮箱 - 力扣(LeetCode)】
2、p1.Id > p2.Id
继续之前,先简单看一下表的连接过程,这个搞懂了,理解WHERE条件就简单了
a. 从驱动表(左表)取出N条记录;
b. 拿着这N条记录,依次到被驱动表(右表)查找满足WHERE条件的记录;
DELETE p1 FROM Person p1, Person p2WHERE p1.Email = p2.Email AND p1.Id > p2.Id正则
【参考:1527. 患某种疾病的患者 - 力扣(LeetCode)】
^DIAB1表示以DIAB1开头 |表示或者 .表示一定有任意一个字符 *表示重复0到无穷多个的前一个字符 第一个\表示转义字符 \s是指空白,包括空格、换行、Tab 缩进等所有的空白 所以.*\sDIAB1表示DIAB1前是空格且空格前有0到无穷多个的任意字符# Write your MySQL query statement belowselect patient_id, patient_name, conditionsfrom Patientswhere conditions rlike '^DIAB1|.*\\sDIAB1'函数
upper lower
【参考:1667. 修复表中的名字 - 力扣(LeetCode)】
【参考:【JMao】简单函数解法+分享刷题经验 - 修复表中的名字 - 力扣(LeetCode)】
【参考:12.3. 字符串函数_MySQL 中文文档】
# Write your MySQL query statement belowSELECT user_id, CONCAT(Upper(Left(name,1)),Lower(substring(name,2))) as name from Users order by user_idconcat
【参考:1484. 按日期分组销售产品 - 力扣(LeetCode)】
【参考:mysql 分组拼接函数 group_concat - 按日期分组销售产品 - 力扣(LeetCode)】
SELECT sell_date, count(distinct product) as 'num_sold', # 组内拼接 group_concat(distinct product # 去重 order by product asc # 分组,按照字典序升序 separator ',') # 间隔 as 'products'from Activitiesgroup by sell_dateorder by sell_dateunion all
union和union all都可以起到关联结果集的作用,区别在于:
- union会自动去除关联的两个结果集中的重复数据,
union all则不会主动去除两个结果集中的重复数据,会展示所有的数据;
列转行
【参考:1795. 每个产品在不同商店的价格 - 力扣(LeetCode)】
【参考:表格-列转行 - 每个产品在不同商店的价格 - 力扣(LeetCode)】
# Write your MySQL query statement belowselect product_id, 'store1' as store, store1 as pricefrom Products where store1 is not nullunion allselect product_id, 'store2' as store, store2 as pricefrom Products where store2 is not nullunion allselect product_id, 'store3' as store, store3 as pricefrom Products where store3 is not null;作者:myaijarvis
游戏编程,一个游戏开发收藏夹~
如果图片长时间未显示,请使用Chrome内核浏览器。
边栏推荐
- Torch learning notes (4) -- torch's dynamic calculation diagram
- 简述服务量化分析体系
- Zero length array
- Torch learning notes (7) -- take lenet as an example for dataload operation (detailed explanation + reserve knowledge supplement)
- User identity used by startup script and login script in group policy
- SSH 远程执行命令简介
- The online customer service system developed by PHP is fully open source without encryption, and supports wechat customer service docking
- Sustainable service business models
- MySQL duplicate check
- HOW TO WRITE A DAILY LAB NOTE?
猜你喜欢

Record: install MySQL on ubuntu18.04

利用可视化结果,点击出现对应的句子

Pytorch introduction to deep learning practice notes 13- advanced chapter of cyclic neural network - Classification

Raft 日志复制

MySQL duplicate check

FBI warning: some people use AI to disguise themselves as others for remote interview

How does GCN use large convolution instead of small convolution? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)

Multifunctional web file manager filestash

application

Xception for deeplab v3+ (including super detailed code comments and original drawing of the paper)
随机推荐
Differential constrained SPFA
How about the Moco model?
Recommend a simple browser tab
HOW TO WRITE A DAILY LAB NOTE?
Compose LazyColumn 顶部添加控件
Does SQL always report foreign key errors when creating tables?
Simulation scheduling problem of SystemVerilog (1)
leetcode:556. 下一个更大元素 III【模拟 + 尽可能少变更】
SQL custom collation
Dart JSON编码器和解码器剖析
虚拟机和开发板互Ping问题
User identity used by startup script and login script in group policy
Okaleido, a multimedia NFT aggregation platform, is about to go online, and a new NFT era may come
235. 二叉搜索树的最近公共祖先【lca模板 + 找路径相同】
Software development freelancer's Road
After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people
Record: solve the problem that MySQL is not an internal or external command environment variable
Dynamic planning -- expansion topics
High concurrency Architecture - separate databases and tables
The online customer service system developed by PHP is fully open source without encryption, and supports wechat customer service docking
