2016-08-01 42 views
-4

我有四張名爲college_list,course_list,branch_list,semester_list的表。並在這些表上應用了JOIN。如何在mysql中選擇DISTINCT記錄uisng在四個表上JOIN

SELECT DISTINCT college_list.clg_name, course_list.crs_name, branch_list.bran_name, semester_list.sem_name FROM college_list 
     LEFT JOIN course_list ON college_list.clg_code = course_list.crs_clg_code 
     LEFT JOIN branch_list ON course_list.crs_code = branch_list.bran_crs_code 
     LEFT JOIN semester_list ON branch_list.bran_crs_code = semester_list.sem_crs_code GROUP BY bran_name 
     UNION ALL 
     SELECT DISTINCT college_list.clg_name, course_list.crs_name, branch_list.bran_name, semester_list.sem_name FROM college_list 
     RIGHT JOIN course_list ON college_list.clg_code = course_list.crs_clg_code 
     LEFT JOIN branch_list ON course_list.crs_code = branch_list.bran_crs_code 
     LEFT JOIN semester_list ON branch_list.bran_crs_code = semester_list.sem_crs_code GROUP BY bran_name 
     UNION ALL 
     SELECT DISTINCT college_list.clg_name, course_list.crs_name, branch_list.bran_name, semester_list.sem_name FROM college_list 
     RIGHT JOIN course_list ON college_list.clg_code = course_list.crs_clg_code 
     RIGHT JOIN branch_list ON course_list.crs_code = branch_list.bran_crs_code 
     LEFT JOIN semester_list ON branch_list.bran_crs_code = semester_list.sem_crs_code GROUP BY bran_name 
     UNION ALL 
     SELECT DISTINCT college_list.clg_name, course_list.crs_name, branch_list.bran_name, semester_list.sem_name FROM college_list 
     RIGHT JOIN course_list ON college_list.clg_code = course_list.crs_clg_code 
     RIGHT JOIN branch_list ON course_list.crs_code = branch_list.bran_crs_code 
     RIGHT JOIN semester_list ON branch_list.bran_crs_code = semester_list.sem_crs_code GROUP BY bran_name 

該查詢結果如下。

enter image description here

但我想要的大學名稱來一次。和其他欄一樣。 那麼如何使用GROUP BY或DISTINCT命令來獲取呢?

+0

分組最好用於這項工作 –

+0

但它不工作 –

+0

你能告訴我如何使用它 –

回答

0

寫代碼,我覺得你的問題將得到解決

SELECT college_list.clg_name, course_list.crs_name, branch_list.bran_name, semester_list.sem_name FROM college_list 
        LEFT JOIN course_list ON college_list.clg_code = course_list.crs_clg_code 
        LEFT JOIN branch_list ON course_list.crs_code = branch_list.bran_crs_code 
        LEFT JOIN semester_list ON branch_list.bran_crs_code = semester_list.sem_crs_code GROUP BY bran_name 
        UNION ALL 
        SELECT DISTINCT college_list.clg_name, course_list.crs_name, branch_list.bran_name, semester_list.sem_name FROM college_list 
        RIGHT JOIN course_list ON college_list.clg_code = course_list.crs_clg_code 
        LEFT JOIN branch_list ON course_list.crs_code = branch_list.bran_crs_code 
        LEFT JOIN semester_list ON branch_list.bran_crs_code = semester_list.sem_crs_code GROUP BY bran_name 
        UNION ALL 
        SELECT DISTINCT college_list.clg_name, course_list.crs_name, branch_list.bran_name, semester_list.sem_name FROM college_list 
        RIGHT JOIN course_list ON college_list.clg_code = course_list.crs_clg_code 
        RIGHT JOIN branch_list ON course_list.crs_code = branch_list.bran_crs_code 
        LEFT JOIN semester_list ON branch_list.bran_crs_code = semester_list.sem_crs_code GROUP BY bran_name 
        UNION ALL 
        SELECT DISTINCT college_list.clg_name, course_list.crs_name, branch_list.bran_name, semester_list.sem_name FROM college_list 
        RIGHT JOIN course_list ON college_list.clg_code = course_list.crs_clg_code 
        RIGHT JOIN branch_list ON course_list.crs_code = branch_list.bran_crs_code 
        RIGHT JOIN semester_list ON branch_list.bran_crs_code = semester_list.sem_crs_code GROUP BY bran_name 

        group by college_list.clg_name 
+0

對不起,它顯示一個錯誤 –

1

因爲我沒有從表中的數據,我不能夠測試我的解決方案。我建議以下查詢:

SELECT * FROM (SELECT college_list.clg_name, course_list.crs_name, branch_list.bran_name, semester_list.sem_name FROM college_list 
     LEFT JOIN course_list ON college_list.clg_code = course_list.crs_clg_code 
     LEFT JOIN branch_list ON course_list.crs_code = branch_list.bran_crs_code 
     LEFT JOIN semester_list ON branch_list.bran_crs_code = semester_list.sem_crs_code 
     UNION ALL 
     SELECT DISTINCT college_list.clg_name, course_list.crs_name, branch_list.bran_name, semester_list.sem_name FROM college_list 
     RIGHT JOIN course_list ON college_list.clg_code = course_list.crs_clg_code 
     LEFT JOIN branch_list ON course_list.crs_code = branch_list.bran_crs_code 
     LEFT JOIN semester_list ON branch_list.bran_crs_code = semester_list.sem_crs_code 
     UNION ALL 
     SELECT DISTINCT college_list.clg_name, course_list.crs_name, branch_list.bran_name, semester_list.sem_name FROM college_list 
     RIGHT JOIN course_list ON college_list.clg_code = course_list.crs_clg_code 
     RIGHT JOIN branch_list ON course_list.crs_code = branch_list.bran_crs_code 
     LEFT JOIN semester_list ON branch_list.bran_crs_code = semester_list.sem_crs_code 
     UNION ALL 
     SELECT DISTINCT college_list.clg_name, course_list.crs_name, branch_list.bran_name, semester_list.sem_name FROM college_list 
     RIGHT JOIN course_list ON college_list.clg_code = course_list.crs_clg_code 
     RIGHT JOIN branch_list ON course_list.crs_code = branch_list.bran_crs_code 
     RIGHT JOIN semester_list ON branch_list.bran_crs_code = semester_list.sem_crs_code) AS TEMP GROUP BY clg_name, crs_name, bran_name, sem_name 

你先結合單獨的查詢,形成一個虛擬的表,然後由所有列該表分組選擇。您希望對整個結果集進行分組,而不是由4個查詢分別進行分組。

+0

看到當我用上面的代碼顯示「College 1College 1College 1大學1大學1大學2 ...「等等......但我想只有不同的學院名稱,即」大學1大學2「 –

+0

好吧,然後由clg_name只分組。但是,除了一個(你不知道哪一個 - 它將在數據庫中隨機選擇)以及除一個(再次隨機選擇)之外的所有分支以及除一個之外的所有學期(也是隨機的選擇)。那麼,爲什麼還要顯示大學名稱以外的其他專欄?也許你只想向大學展示至少一門課程?那麼你需要另一個查詢。如果您需要了解所有大學所有分校的所有課程的學期 - 這是正確的問題。 –