1.coalesce
返回其参数中第一个非空表达式。
如果所有自变量均为 NULL,则 COALESCE 返回 NULL 值。
语法
COALESCE ( expression [ ,...n ] )
参数
expression
任何类型的表达式
2.CASE When 用法,记得去看一下。
3.GROUP_CONCAT()函数的用法:
如果我们要查找id为2的学生选课情况 mysql> SELECT student_id, courses_id FROM student_courses WHERE student_id=2; +------------+------------+
| student_id | courses_id | +------------+------------+
| 2 | 3 | | 2 | 4 | | 2 | 5 |
而如果采用GROUP_CONCAT()函数和GROUP BY语句就显得非常简单了 mysql> SELECT student_id, GROUP_CONCAT(courses_id) AS courses FROM student_courses WHERE student_id=2 GROUP BY student_id; +------------+---------+
| student_id | courses | +------------+---------+
| 2 | 3,4,5 |
除此之外,还可以对这个组的值来进行排序再连接成字符串,例如按courses_id降序来排: mysql> SELECT student_id, GROUP_CONCAT(courses_id ORDER BY courses_id DESC) AS courses FROM student_courses WHERE student_id=2 GROUP BY student_id; +------------+---------+
| student_id | courses | +------------+---------+
| 2 | 5,4,3 | +------------+---------+