2012-08-22 92 views
0

我有兩個表課程和科目。這是一個一對多的比率在每節課可以在導致類似「名稱」主題1個或多個科目進行分類:數學,科學,社會研究如何將多個表中的一條記錄組合起來

這將返回3個記錄:
名稱,數學
名稱,科學
名,社會科學

這導致我通過循環和組合以及打亂了我的搜索計數
我怎樣才能得到一個記錄所有三個呢?所以我的搜索計數是準確的,我不需要額外的PHP代碼來檢查名稱是否相同並收集額外的主題?

我試過基本

SELECT * FROM lesson INNER JOIN subjects ON subject.id = lesson.subject 

但是這導致3項

編輯
我的查詢更復雜,比我帶領。我有一箇中間表,用於跟蹤上面的兩個表格及其關係
此查詢用於搜索。這是我的。

SELECT name, subject   
FROM lesson As l 
INNER JOIN lesson_sub As ls 
    ON ls.lesson_id = l.id 
INNER JOIN subjects As s 
    ON s.id = ls.subject_id 
WHERE CONCAT(l.name, s.subject) LIKE '%KEYWORD%' AND s.id = SUBJECT_ID 
+0

你有沒有想過使用一個框架像[CakePHP的] (http://cakephp.org/)或[CodeIgnighter](http://codeignighter.com/)爲您提供實用方法,爲您實現這種事情? – tadman

回答

2

可以使用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) 
+0

當使用GROUP_CONCAT時,我正在使用組函數查詢無效 –

+0

發佈您正在使用的查詢。用測試用例更新我的答案。 – LSerni

+0

非常棒!唯一的區別是我有很多列選擇,如果我在外部做SELECT *選擇它很好,但如果我做CONCAT(*)喜歡,那麼它不起作用。任何想法CONCAT從內部選擇返回的所有字段? –

0

爲什麼內連接,如果你只是想要主題名稱?現在

SELECT subject 
FROM lesson 

,如果你想要的是至少有一個課科目,那麼你可以簡單地做

SELECT DISTINCT subject 
FROM lesson 
INNER JOIN subjects ON subject.id = lesson.subject 
0

它升像你這樣的人希望每人有一個記錄,那就是每name

這裏有一個辦法做到這一點:

 SELECT name, count(*) subject_count 
     FROM lesson 
    INNER JOIN subjects ON subject.id = lesson.subject 
    GROUP BY name 
    ORDER BY name 

如果需要在名字的記錄中顯示的主題,試試這個:

 SELECT name, 
      count(*) subject_count, 
      group_concat(subject) subjects 
     FROM lesson 
    INNER JOIN subjects ON subject.id = lesson.subject 
    GROUP BY name 
    ORDER BY name 
+0

小組是否在哪裏工作? –

+0

當然'GROUP BY'與'WHERE'一起使用。 –

相關問題