Mysql的GROUP_CONCAT()函数使用方法
程序员文章站
2022-10-27 16:29:20
语法: group_concat([distinct] expr [,expr ...][order by {unsign...
语法:
group_concat([distinct] expr [,expr ...][order by {unsigned_integer | col_name | expr}[asc | desc] [,col_name ...]][separator str_val])
下面演示一下这个函数,先建立一个学生选课表student_courses,并填充一些测试数据。
sql代码
create table student_courses (
student_id int unsigned not null,
courses_id int unsigned not null,
key(student_id)
);
insert into student_courses values (1, 1), (1, 2), (2, 3), (2, 4), (2, 5);
若要查找学生id为2所选的课程,则使用下面这条sql:
sql代码
mysql> select student_id, courses_id from student_courses where student_id=2;
+------------+------------+
| student_id | courses_id |
+------------+------------+
| 2 | 3 |
| 2 | 4 |
| 2 | 5 |
+------------+------------+
3 rows in set (0.00 sec)
输出结果有3条记录,说明学生id为2的学生选了3、4、5这3门课程。
放在php里,必须用一个循环才能取到这3条记录,如下所示:
php代码
foreach ($pdo->query("select student_id, courses_id from student_courses where student_id=2") as $row) {
$result[] = $row['courses_id'];
}
而如果采用group_concat()函数和group by语句就显得非常简单了,如下所示:
sql代码
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 |
+------------+---------+
1 row in set (0.00 sec)
这样php里处理就简单了:
php代码
$row = $pdo->query("select student_id, group_concat(courses_id) as courses from student_courses where student_id=2 group by student_id");
$result = explode(',', $row['courses']);
分隔符还可以自定义,默认是以“,”作为分隔符,若要改为“|||”,则使用separator来指定,例如:
sql代码
select student_id, group_concat(courses_id separator '|||') as courses from student_courses where student_id=2 group by student_id;
除此之外,还可以对这个组的值来进行排序再连接成字符串,例如按courses_id降序来排:
sql代码
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;
group_concat([distinct] expr [,expr ...][order by {unsigned_integer | col_name | expr}[asc | desc] [,col_name ...]][separator str_val])
下面演示一下这个函数,先建立一个学生选课表student_courses,并填充一些测试数据。
sql代码
复制代码 代码如下:
create table student_courses (
student_id int unsigned not null,
courses_id int unsigned not null,
key(student_id)
);
insert into student_courses values (1, 1), (1, 2), (2, 3), (2, 4), (2, 5);
若要查找学生id为2所选的课程,则使用下面这条sql:
sql代码
复制代码 代码如下:
mysql> select student_id, courses_id from student_courses where student_id=2;
+------------+------------+
| student_id | courses_id |
+------------+------------+
| 2 | 3 |
| 2 | 4 |
| 2 | 5 |
+------------+------------+
3 rows in set (0.00 sec)
输出结果有3条记录,说明学生id为2的学生选了3、4、5这3门课程。
放在php里,必须用一个循环才能取到这3条记录,如下所示:
php代码
复制代码 代码如下:
foreach ($pdo->query("select student_id, courses_id from student_courses where student_id=2") as $row) {
$result[] = $row['courses_id'];
}
而如果采用group_concat()函数和group by语句就显得非常简单了,如下所示:
sql代码
复制代码 代码如下:
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 |
+------------+---------+
1 row in set (0.00 sec)
这样php里处理就简单了:
php代码
复制代码 代码如下:
$row = $pdo->query("select student_id, group_concat(courses_id) as courses from student_courses where student_id=2 group by student_id");
$result = explode(',', $row['courses']);
分隔符还可以自定义,默认是以“,”作为分隔符,若要改为“|||”,则使用separator来指定,例如:
sql代码
复制代码 代码如下:
select student_id, group_concat(courses_id separator '|||') as courses from student_courses where student_id=2 group by student_id;
除此之外,还可以对这个组的值来进行排序再连接成字符串,例如按courses_id降序来排:
sql代码
复制代码 代码如下:
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;
上一篇: ubuntu系统快速搭建开发环境
下一篇: 关于Intype一些小问题的解决办法