发现mysql又一强悍,group_concat的使用
对于group_concat的功能,其实在很早之前就有需求,原先的老方法是直接left jion 取出数据,然后通过php分离组合,浪费大大量的系统资源,而且分页和排序都比较复杂。
今天又有这一需求,就在网上找相关资料,找了很多关键字,都找不到,想到了concat的东西,然后加上group,果然,mysql早有此功能,让我又验证了一个道理,没有做不到,只有想不到,我们作为开发者所想的,人家系统早就想好了,所以,大家在开发过程中,大胆的想,大胆的去找。
GROUP_CONCAT()是MySQL数据库提供的一个函数,通常跟GROUP BY一起用,具体可参考MySQL官方文挡:http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
函数的语法:
GROUP_CONCAT([DISTINCT] expr [,expr ...]
[ORDER BY {unsigned_integer | col_name | expr}
[ASC | DESC] [,col_name ...]]
[SEPARATOR str_val])
下面就演示一下这个函数所带来的方便之处,首先建立一个学生选课表student_courses,并填充一些测试数据。
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:
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语句就显得非常简单了,如下所示:
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来指定,例如:
mysql> SELECT student_id, GROUP_CONCAT(courses_id SEPARATOR ‘|||’) 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)
除此之外,还可以对这个组的值来进行排序再连接成字符串,例如按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 |
+————+———+
1 row IN SET (0.02 sec)
对于group_concat的功能,其实在很早之前就有需求,原先的老方法是直接left jion 取出数据,然后通过php分离组合,浪费大大量的系统资源,而且分页和排序都比较复杂。
今天又有这一需求,就在网上找相关资料,找了很多关键字,都找不到,想到了concat的东西,然后加上group,果然,mysql早有此功能,让我又验证了一个道理,没有做不到,只有想不到,我们作为开发者所想的,人家系统早就想好了,所以,大家在开发过程中,大胆的想,大胆的去找。
GROUP_CONCAT()是MySQL数据库提供的一个函数,通常跟GROUP BY一起用,具体可参考MySQL官方文挡:http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
函数的语法:
GROUP_CONCAT([DISTINCT] expr [,expr ...]
[ORDER BY {unsigned_integer | col_name | expr}
[ASC | DESC] [,col_name ...]]
[SEPARATOR str_val])
下面就演示一下这个函数所带来的方便之处,首先建立一个学生选课表student_courses,并填充一些测试数据。
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:
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语句就显得非常简单了,如下所示:
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来指定,例如:
mysql> SELECT student_id, GROUP_CONCAT(courses_id SEPARATOR ‘|||’) 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)
除此之外,还可以对这个组的值来进行排序再连接成字符串,例如按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 |
+————+———+
1 row IN SET (0.02 sec)
04月 7th, 2008 at 3:55 am
Looks perfectly straight to me. Of course, I’m also the type of person who allegedly [ahem! allegedly] agree with everything.
04月 9th, 2008 at 6:13 pm
I can’t stand the way some people express their mind – it sound terrible. Is a lot of cussing necessary, guys?