我有四張名爲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
該查詢結果如下。
但我想要的大學名稱來一次。和其他欄一樣。 那麼如何使用GROUP BY或DISTINCT命令來獲取呢?
分組最好用於這項工作 –
但它不工作 –
你能告訴我如何使用它 –