当前位置:网站首页>【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内核浏览器。
边栏推荐
- The more you talk, the more your stupidity will be exposed.
- What is SQL get connection
- 虚拟机和开发板互Ping问题
- SSH 远程执行命令简介
- How to read the source code [debug and observe the source code]
- Why can deeplab v3+ be a God? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)
- 【学术相关】顶级论文创新点怎么找?中国高校首次获CVPR最佳学生论文奖有感...
- 2022.02.11
- Shell script return value with which output
- Gao Qing, Beijing University of Aeronautics and Astronautics: CIM is a natural quantum computing platform for graph data processing
猜你喜欢

虚拟机和开发板互Ping问题

Record: install MySQL on ubuntu18.04
![leetcode:11. Container with the most water [double pointer + greed + remove the shortest board]](/img/d4/cbbaec40119be6cb5594899e348261.png)
leetcode:11. Container with the most water [double pointer + greed + remove the shortest board]

leetcode:11. 盛最多水的容器【雙指針 + 貪心 + 去除最短板】

Add control at the top of compose lazycolumn

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

Simulation scheduling problem of SystemVerilog (1)

SQL custom collation

Record: MySQL changes the time zone

Does SQL always report foreign key errors when creating tables?
随机推荐
shell 脚本中关于用户输入参数的处理
High concurrency Architecture - distributed search engine (ES)
Pan for in-depth understanding of the attention mechanism in CV
[leetcode周赛]第300场——6110. 网格图中递增路径的数目-较难
How about the Moco model?
ActiveMQ的基础
Zhengda futures news: soaring oil prices may continue to push up global inflation
Recommend a simple browser tab
Shell script return value with which output
Record: MySQL changes the time zone
平淡的生活里除了有扎破皮肤的刺,还有那些原本让你魂牵梦绕的诗与远方
High concurrency architecture cache
Unity2018 to wechat games without pictures
SQL: special update operation
Okaleido, a multimedia NFT aggregation platform, is about to go online, and a new NFT era may come
Raft 日志复制
__ Weak and__ The difference between blocks
What does foo mean in programming?
Mysql45 lecture learning notes (II)
Compose LazyColumn 顶部添加控件
