可以使用GROUP_CONCAT()函數與JOIN查詢:
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學生 - > GROUP BY student_name;
或者:
的mysql> SELECT student_name, - > GROUP_CONCAT(DISTINCT test_score - > ORDER BY test_score DESC SEPARATOR'「) - >從學生 - > 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;
TEST
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)
更復雜的TEST(具有中間表)
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)
你有沒有想過使用一個框架像[CakePHP的] (http://cakephp.org/)或[CodeIgnighter](http://codeignighter.com/)爲您提供實用方法,爲您實現這種事情? – tadman