1. ORDER BY FIELD() custom sorting logic
Sorting ORDER BY In addition to using ASC and DESC, you can also use **ORDER BY FIELD(str,str1,...)** to achieve sorting by customizing strings/numbers. Here is an example of the order_diy table, structure and table data display:
Two, CASE expression
The "case when then else end" expression is very powerful and can help us solve if elseif else
this problem. Here we continue to use the order_diy table as an example. If we want to add a level column to the order_diy table, according to the money judgment, it is higher than 60, and it is higher than 30. , and the rest show low-level, sql as follows:
3. Usage of EXISTS
I guess everyone should use less of the keyword exists in daily development, and it is estimated that in queries are used more. Here is an introduction to the usage of exists, citing the official website documentation:
exists is followed by a subquery statement, its function is "according to the data of the main query, each row is placed in the subquery for conditional verification, and according to the verification result (TRUE or FALSE), if TRUE, the row data will be retained" , the following uses emp table and dept table as examples, table structure and data display
SELECT *,
case when money > 60 then '高级'
when money > 30 then '中级'
else '低级' END level
from order_diy;