您可以在 JOIN 查询中使用 GROUP_CONCAT() 函数:
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
GROUP_CONCAT(expr)
此函数返回一个字符串结果,其中包含来自组的串联非 NULL 值。如果没有非 NULL 值,则返回 NULL。完整的语法如下:
GROUP_CONCAT([DISTINCT] expr [,expr ...] [ORDER BY {unsigned_integer | col_name | expr} [ASC | DESC] [,col_name ...]] [SEPARATOR str_val])
mysql> SELECT student_name, -> GROUP_CONCAT(test_score) -> FROM student -> GROUP BY student_name;
或者:
mysql> SELECT student_name, -> GROUP_CONCAT(DISTINCT test_score -> ORDER BY test_score DESC SEPARATOR ' ') -> FROM student -> GROUP BY student_name;
所以:
SELECT lesson.name, GROUP_CONCAT(subjects.name SEPARATOR ', ')
FROM lesson JOIN subjects ON (subject.id = lesson.subject)
GROUP BY lesson.name;
测试
CREATE TABLE lesson ( name varchar (20), subject integer );
CREATE TABLE subjects ( id integer, name varchar(20) );
INSERT INTO subjects VALUES ( 1, 'Math' ), ( 2, 'Physics' ), ( 3, 'Chemistry' );
INSERT INTO lesson VALUES ( 'Lesson A', 1 );
INSERT INTO lesson VALUES ( 'Lesson A', 2 );
INSERT INTO lesson VALUES ( 'Lesson A', 3 );
INSERT INTO lesson VALUES ( 'Lesson B', 2 );
INSERT INTO lesson VALUES ( 'Lesson B', 3 );
INSERT INTO lesson VALUES ( 'Lesson C', 1 );
SELECT lesson.name, GROUP_CONCAT(subjects.name SEPARATOR ', ')
FROM lesson JOIN subjects ON (subjects.id = lesson.subject)
GROUP BY lesson.name;
+----------+--------------------------------------------+
| name | GROUP_CONCAT(subjects.name SEPARATOR ', ') |
+----------+--------------------------------------------+
| Lesson A | Math, Chemistry, Physics |
| Lesson B | Chemistry, Physics |
| Lesson C | Math |
+----------+--------------------------------------------+
3 rows in set (0.00 sec)
更复杂的测试(带中间表)
CREATE TABLE lesson ( id integer, name varchar (20) );
CREATE TABLE subjects ( id integer, name varchar(20) );
CREATE TABLE lesson_sub ( lesson_id integer, subject_id integer );
INSERT INTO subjects VALUES ( 1, 'Math' ), ( 2, 'Physics' ), ( 3, 'Chemistry' );
INSERT INTO lesson VALUES ( 1, 'Lesson A' ), ( 2, 'Lesson B' ), ( 3, 'Lesson C' );
INSERT INTO lesson_sub VALUES (1,1), (1,2),(1,3),(2,2),(2,3),(3,1);
SELECT lesson.name, GROUP_CONCAT(subjects.name SEPARATOR ', ') AS subjects
FROM lesson_sub JOIN lesson ON ( lesson.id = lesson_sub.lesson_id )
JOIN subjects ON (subjects.id = lesson_sub.subject_id)
WHERE CONCAT(lesson.name, subjects.name) LIKE '%Chem%'
GROUP BY lesson.name;
SELECT name, subjects FROM (
SELECT lesson.name, GROUP_CONCAT(subjects.name SEPARATOR ', ') AS subjects
FROM lesson_sub JOIN lesson ON ( lesson.id = lesson_sub.lesson_id )
JOIN subjects ON (subjects.id = lesson_sub.subject_id)
GROUP BY lesson.name ) AS lesson_clear
WHERE CONCAT(name, subjects) LIKE '%Chem%';
+----------+--------------------------------------------+
| name | GROUP_CONCAT(subjects.name SEPARATOR ', ') |
+----------+--------------------------------------------+
| Lesson A | Chemistry |
| Lesson B | Chemistry |
+----------+--------------------------------------------+
2 rows in set (0.00 sec)
+----------+--------------------------+
| name | subjects |
+----------+--------------------------+
| Lesson A | Physics, Math, Chemistry |
| Lesson B | Physics, Chemistry |
+----------+--------------------------+
2 rows in set (0.00 sec)