当前位置:网站首页>[leetcode] [SQL] notes
[leetcode] [SQL] notes
2022-07-03 19:00:00 【Game programming】
code Format specification : All keywords are capitalized , Align keywords to the right , Clause indent
The column name should be the same as the column name in the table ( Case case )
SELECT nameFROM customer CWHERE C.id NOT IN ( SELECT C1.id FROM customer C1 WHERE C1.referee_id = 2 );
Reference material :
Official documents :【 Reference resources :MySQL :: MySQL 5.7 Reference manual :: 13 SQL sentence 】
【 Reference resources :MySQL Chinese document | MySQL Chinese net 】
【 Reference resources :SQL The predicate in - You know 】
Topics are categorized by type , Record common and error prone types
IS NULL
【 Reference resources :584. Looking for user references 【 Finer than official , A thousand words of dry goods !】( Ternary operation ,NULL) - Looking for user references - Power button (LeetCode)】
【 Reference resources :584. Looking for user references - Simple - Power button (LeetCode)】
SELECT name FROM customer WHERE referee_id != 2 OR referee_id IS NULL;
Multi-table query
【 Reference resources :183. Customers who never order - Power button (LeetCode)】
- Subquery
select customers.name as 'Customers' # as Alias from customerswhere customers.id not in( select customerid from orders);# First from orders To find out in the customerid
- Link query
【 Reference resources : The illustration SQL Interview questions : Find data that is not in the table - Customers who never order - Power button (LeetCode)】
SELECT c.Name as CustomersFROM Customers as cleft join Orders as o on c.Id=o.CustomerIdwhere o.Id IS NULL
IF expression
IF( expr1 , expr2 , expr3 )
【 Reference resources :1873. Calculate special bonuses - Power button (LeetCode)】
【 Reference resources :MySQL, 7 A solution - Calculate special bonuses - Power button (LeetCode)】
SELECT employee_id, IF( employee_id%2=1 and name not like 'M%', salary, 0 ) as bonus FROM EmployeesORDER by employee_id
【 Reference resources :627. Changing gender - Power button (LeetCode)】
UPDATE Salary set sex=IF(sex='f','m','f')
CASE WHEN END
【 Reference resources :627. Changing gender - Power button (LeetCode)】
UPDATE salarySET sex = CASE sex WHEN 'm' THEN 'f' ELSE 'm' END
Self join
【 Reference resources :196. Delete duplicate email - Power button (LeetCode)】
【 Reference resources : Yes 「 official 」 In the solution “delete” and “>” The explanation of , recommend ! - Delete duplicate email - Power button (LeetCode)】
2、p1.Id > p2.Id
Before continuing , First, let's take a brief look at the connection process of the table , I understand this , understand WHERE The conditions are simple
a. From the drive table ( The left table ) Take out N Bar record ;
b. Take this. N Bar record , In turn, go to the driven table ( Right table ) Find satisfaction WHERE Record of conditions ;
DELETE p1 FROM Person p1, Person p2WHERE p1.Email = p2.Email AND p1.Id > p2.Id
Regular
【 Reference resources :1527. A patient with a disease - Power button (LeetCode)】
^DIAB1 Said to DIAB1 start | Express or . Indicates that there must be any character * It means repetition 0 To an infinite number of previous characters first \ Indicates the escape character \s It's blank , Including Spaces 、 Line break 、Tab Indent, all the blanks therefore .*\sDIAB1 Express DIAB1 There is a space before and 0 To an infinite number of arbitrary characters
# Write your MySQL query statement belowselect patient_id, patient_name, conditionsfrom Patientswhere conditions rlike '^DIAB1|.*\\sDIAB1'
function
upper lower
【 Reference resources :1667. Fix the name in the table - Power button (LeetCode)】
【 Reference resources :【JMao】 Simple function solution + Share the experience of problem brushing - Fix the name in the table - Power button (LeetCode)】
【 Reference resources :12.3. String function _MySQL Chinese document 】
# Write your MySQL query statement belowSELECT user_id, CONCAT(Upper(Left(name,1)),Lower(substring(name,2))) as name from Users order by user_id
concat
【 Reference resources :1484. Sell products by date - Power button (LeetCode)】
【 Reference resources :mysql Group splicing function group_concat - Sell products by date - Power button (LeetCode)】
SELECT sell_date, count(distinct product) as 'num_sold', # Intra group splicing group_concat(distinct product # duplicate removal order by product asc # grouping , In ascending order according to the dictionary separator ',') # interval as 'products'from Activitiesgroup by sell_dateorder by sell_date
union all
union and union all Can play the role of associating result sets , The difference lies in :
- union It will automatically remove the duplicate data in the associated two result sets ,
union all Will not actively remove duplicate data in the two result sets , All the data will be displayed ;
Column turned
【 Reference resources :1795. The price of each product in different stores - Power button (LeetCode)】
【 Reference resources : form - Column turned - The price of each product in different stores - Power button (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;
author :myaijarvis
Game programming , A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .
边栏推荐
- Boost.Asio Library
- Reading a line from ifstream into a string variable
- Flutter网络和数据存储框架搭建 -b1
- What does a really excellent CTO look like in my eyes
- Flutter network and data storage framework construction-b1
- CV in transformer learning notes (continuously updated)
- 我们做了一个智能零售结算平台
- 达梦数据库的物理备份和还原简解
- High concurrency architecture cache
- Torch learning notes (7) -- take lenet as an example for dataload operation (detailed explanation + reserve knowledge supplement)
猜你喜欢
多媒体NFT聚合平台OKALEIDO即将上线,全新的NFT时代或将来临
【疾病识别】基于matlab GUI机器视觉肺癌检测系统【含Matlab源码 1922期】
235. Ancêtre public le plus proche de l'arbre de recherche binaire [modèle LCA + même chemin de recherche]
EGO Planner代码解析bspline_optimizer部分(2)
【LeetCode】【SQL】刷题笔记
组策略中开机脚本与登录脚本所使用的用户身份
The online customer service system developed by PHP is fully open source without encryption, and supports wechat customer service docking
Add control at the top of compose lazycolumn
Flutter网络和数据存储框架搭建 -b1
Getting started with JDBC
随机推荐
Understanding of database architecture
[combinatorics] dislocation problem (recursive formula | general term formula | derivation process)*
Leetcode: 11. Récipient contenant le plus d'eau [double pointeur + cupidité + enlèvement de la plaque la plus courte]
Zero length array
简述服务量化分析体系
High concurrency Architecture - distributed search engine (ES)
平淡的生活里除了有扎破皮肤的刺,还有那些原本让你魂牵梦绕的诗与远方
[academic related] how to find the innovation of top papers? Chinese universities won the CVPR Best Student Thesis Award for the first time
Differential constrained SPFA
Torch learning notes (5) -- autograd
leetcode:11. Container with the most water [double pointer + greed + remove the shortest board]
Torch learning notes (4) -- torch's dynamic calculation diagram
How many convolution methods does deep learning have? (including drawings)
How about the Moco model?
How to quickly view the inheritance methods of existing models in torchvision?
CV in transformer learning notes (continuously updated)
User identity used by startup script and login script in group policy
Why can deeplab v3+ be a God? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)
High concurrency Architecture - read write separation
application