Share advanced sql writing

1.
 ORDER BY FIELD() Custom sorting logic
 $sql = "SELECT * from edu_class_node ORDER BY FIELD(title,'Cows and horses','New commodity','I am a student','New market','22',' Haha','note title','yes')"; 
$data = Db::query($sql); 
sorting ORDER BY in MySql In addition to ASC and DESC, you can also use ORDER BY FIELD(str,str1, ...) Customize strings/numbers to achieve sorting 

2. Case expression 
$sql = "SELECT case when uid > 70 then 'advanced' when uid > 1 then 'intermediate' else 'low' END FROM edu_class_node"; 
if When encountering null or data that does not exist in the result set, return 0, and then sort according to ascending order 
$data = Db::query($sql); 
case when then else end expression is very powerful and can help us solve if elseif else this kind of problem

three,

exists usage

$sql = " SELECT * from edu_class_node e where exists (SELECT * from edu_stu_note p where e.section_id = p.section_id and e.course_id != p.course_id)";

$data = Db::query($sql);

exists is followed by a subquery statement. Its function is to put each row in the subquery for conditional verification according to the data of the main query. According to the verification result (TRUE or FALSE), if TRUE, the row data will be retained.

Four,

group_concat(expr) group connection function

$sql = " SELECT id,uid, GROUP_CONCAT(title ORDER BY course_id desc SEPARATOR '-')from edu_class_node GROUP BY title ORDER BY NULL";

$data = Db::query($sql);

GROUP_CONCAT(expr) The group connection function can return the string connection form of the specified field after grouping, and can specify the sorting logic and connection string. The default is English comma connection. The specified group connection title field we use here is sorted by id, set The connection string is -  

Five, order by null value sorting

ORDER BY IF(ISNULL(title), 1, 0)

$sql = "SELECT * FROM edu_class_node ORDER BY IF(ISNULL(class_id), 0, 1),course_id "; $data = Db::query($sql);

ORDER BY is followed by the name of the field we want to sort. When there is a null value in the field, we can use the ORDER BY IF(ISNULL(class_id), 1, 0) syntax to convert the null value into 0 or 1, so as to put the null value in The effect of sorting before or after

six,

with rollup Group statistical data based on statistical summary
$sql = "SELECT title, SUM(course_id) as total FROM edu_class_node GROUP BY title WITH ROLLUP";

$data = Db::query($sql); GROUP BY title WITH ROLLUP statement, the last column of the query result shows the summary result of the group statistics. But the title field is finally displayed as null, we can pass the coalesce(val1, val2, ...) function, this function will return the first non-null parameter in the parameter list

seven,

On duplicate key update if it exists, it will be updated, if it does not exist, it will be inserted

$sql = " INSERT INTO `edu_class_node` (`title`, `content`,`section_id`)VALUES ('title', 'content',16) on duplicate key update title = 'title and content',content='1111 '"; 
$data = Db::query($sql); 
In MySql, the on duplicate key update syntax is used to implement the logic of updating if it exists, and inserting it if it does not exist. When inserting or updating, it will judge according to the primary key index or unique index in the table. If the primary key index or unique index conflicts, it will execute the assignment statement after on duplicate key update

Guess you like

Origin blog.csdn.net/qq_58778333/article/details/130644149