当前位置:网站首页>[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
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 .

原网站

版权声明
本文为[Game programming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/184/202207031855292373.html