当前位置:网站首页>[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 NULLIF 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' ENDSelf 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.IdRegular
【 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_idconcat
【 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_dateunion 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 .
边栏推荐
- SSH 远程执行命令简介
- Record: install MySQL on ubuntu18.04
- FBI警告:有人利用AI换脸冒充他人身份进行远程面试
- NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
- Flutter网络和数据存储框架搭建 -b1
- 【光学】基于matlab介电常数计算【含Matlab源码 1926期】
- TypeScript 官网教程
- How about the Moco model?
- HOW TO WRITE A DAILY LAB NOTE?
- 【疾病识别】基于matlab GUI机器视觉肺癌检测系统【含Matlab源码 1922期】
猜你喜欢

NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon

【LeetCode】【SQL】刷题笔记

How many convolution methods does deep learning have? (including drawings)

Record the errors reported when running fluent in the simulator

Getting started with JDBC

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

Record: writing MySQL commands

Smart wax therapy machine based on STM32 and smart cloud

We have built an intelligent retail settlement platform

Dart JSON编码器和解码器剖析
随机推荐
【LeetCode】【SQL】刷题笔记
硬盘监控和分析工具:Smartctl
Scrapy爬虫框架
How does GCN use large convolution instead of small convolution? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)
Flask generates swagger documents
Find the median of two positive arrays
How to disable the clear button of ie10 insert text box- How can I disable the clear button that IE10 inserts into textboxes?
In addition to the prickles that pierce your skin, there are poems and distant places that originally haunt you in plain life
Implementation of cqrs architecture mode under Kratos microservice framework
How to read the source code [debug and observe the source code]
Hard disk monitoring and analysis tool: smartctl
变化是永恒的主题
2022.02.11
After nohup NPM start &, close the shell window directly, and the process closes accordingly
[Yu Yue education] world reference materials of Microbiology in Shanghai Jiaotong University
How about the Moco model?
[leetcode周赛]第300场——6110. 网格图中递增路径的数目-较难
Le changement est un thème éternel
leetcode:11. 盛最多水的容器【雙指針 + 貪心 + 去除最短板】
Analysis of dart JSON encoder and decoder