当前位置:网站首页>【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 (2) -- 11 common operation modes of tensor
- Why can deeplab v3+ be a God? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)
- 235. The nearest common ancestor of the binary search tree [LCA template + same search path]
- 组策略中开机脚本与登录脚本所使用的用户身份
- 042. (2.11) do it when it's time to do it
- Okaleido, a multimedia NFT aggregation platform, is about to go online, and a new NFT era may come
- 235. Ancêtre public le plus proche de l'arbre de recherche binaire [modèle LCA + même chemin de recherche]
- 虚拟机和开发板互Ping问题
- What is SQL get connection
- Pytorch introduction to deep learning practice notes 13- advanced chapter of cyclic neural network - Classification
猜你喜欢

【疾病识别】基于matlab GUI机器视觉肺癌检测系统【含Matlab源码 1922期】

【Proteus仿真】用24C04与1602LCD设计的简易加密电子密码锁

Help change the socket position of PCB part

Torch learning notes (3) -- univariate linear regression model (self training)

22.2.14 -- station B login with code -for circular list form - 'no attribute' - 'needs to be in path selenium screenshot deviation -crop clipping error -bytesio(), etc

Ping problem between virtual machine and development board

【学术相关】顶级论文创新点怎么找?中国高校首次获CVPR最佳学生论文奖有感...

Implementation of cqrs architecture mode under Kratos microservice framework

Chisel tutorial - 06 Phased summary: implement an FIR filter (chisel implements 4-bit FIR filter and parameterized FIR filter)

Simulation scheduling problem of SystemVerilog (1)
随机推荐
【光学】基于matlab介电常数计算【含Matlab源码 1926期】
【学术相关】顶级论文创新点怎么找?中国高校首次获CVPR最佳学生论文奖有感...
Usage of laravel conditional array in
Getting started with JDBC
多媒体NFT聚合平台OKALEIDO即将上线,全新的NFT时代或将来临
[leetcode weekly race] game 300 - 6110 Number of incremental paths in the grid graph - difficult
The more you talk, the more your stupidity will be exposed.
Torch learning notes (2) -- 11 common operation modes of tensor
Reading a line from ifstream into a string variable
The online customer service system developed by PHP is fully open source without encryption, and supports wechat customer service docking
235. The nearest common ancestor of the binary search tree [LCA template + same search path]
041. (2.10) talk about manpower outsourcing
High concurrency Architecture - separate databases and tables
【光学】基于matlab涡旋光产生【含Matlab源码 1927期】
Xception for deeplab v3+ (including super detailed code comments and original drawing of the paper)
Integrated easy to pay secondary domain name distribution system
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
KINGS
High concurrency architecture cache
Shell script return value with which output
