惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Security Archives - TechRepublic
MongoDB | Blog
MongoDB | Blog
量子位
博客园 - 叶小钗
罗磊的独立博客
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
MyScale Blog
MyScale Blog
GbyAI
GbyAI
Help Net Security
Help Net Security
Y
Y Combinator Blog
Engineering at Meta
Engineering at Meta
Hacker News - Newest:
Hacker News - Newest: "LLM"
Latest news
Latest news
H
Hacker News: Front Page
Blog — PlanetScale
Blog — PlanetScale
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
Scott Helme
Scott Helme
S
Schneier on Security
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
S
Security @ Cisco Blogs
Recorded Future
Recorded Future
S
Securelist
博客园 - Franky
Application and Cybersecurity Blog
Application and Cybersecurity Blog
A
About on SuperTechFans
N
News and Events Feed by Topic
AI
AI
T
Tenable Blog
N
News | PayPal Newsroom
C
Cybersecurity and Infrastructure Security Agency CISA
V
V2EX - 技术
T
Threat Research - Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
L
LINUX DO - 热门话题
N
Netflix TechBlog - Medium
S
SegmentFault 最新的问题
T
The Blog of Author Tim Ferriss
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google Online Security Blog
Google Online Security Blog
S
Security Affairs
Webroot Blog
Webroot Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 三生石上(FineUI控件)
C
Comments on: Blog
G
GRAHAM CLULEY

博客园 - window07

Hibernate缓存机制 Facebook 的 Scaling Out 经验 软件架构设计 mysql的FLUSH句法(清除缓存) 优化MySQL数据库性能 MySQL系统变量interactive_timeout 与 wait_timeout INSERT … ON DUPLICATE KEY UPDATE Howto change runtime variables without restart MySQL Server MySQL 5.1新特性之事件调度器(Event Scheduler) MYSQL命令行导出XML格式数据 - window07 - 博客园 Mysql参数配置优化说明 MySQL 备份和恢复 error: Failed dependencies: MySQLconflicts flash的socket连接安全策略文件 缓存系统MemCached的Java客户端优化历程 使用java.util.zip对字符串进行压缩和解压缩 Java和flash通信中数据的zlib压缩与解压缩 反序列化时出现“base-64 字符数组的无效长度”错误提示的解决 关于GC垃圾回收 - window07
GROUP_CONCAT()妙用
window07 · 2009-11-04 · via 博客园 - window07

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)

AddThis