(Notes) Record the usage of some mysql encountered

Record the usage of some mysql encountered

1. Usage of CASE WHEN

select p.id,(
case p.platform_id
 when 1 then '天猫' 
 when 2 then '京东' 
 when 3 then '拼多多' 
 else '其他' end) 平台
  from platform p

Output result:
Insert picture description here

2. The usage of Order By multiple fields

//sort 没有定义升降序默认升序
SELECT id,age,sort FROM `user` ORDER BY sort,age DESC

The final result is first age in descending order, then sort in ascending order
Insert picture description here

// 排序最后会按照第一个 sort 的desc来排序
SELECT id,age,sort FROM user ORDER BY sort DESC, age DESC, id DESC

Insert picture description here
The final result is first id in descending order, then age in descending order, and finally sort in descending order

Use of if() function in mysql

If() in mysql is equivalent to ternary operation, if(condition, input is satisfied, output is not satisfied)

Example: If comments <1, comments =0, otherwise comments = comments -1

update `info` set `comments` = IF(`comments`<1, 0, `comments`-1) WHERE `id` = 32

Guess you like

Origin blog.csdn.net/qq_39004843/article/details/105932028