如何在四个表中选择mysql uisng JOIN中的DISTINCT记录

时间:2016-08-01 07:27:25

标签: php mysql database select

我有四个名为college_list,course_list,branch_list,semester_list的表。并在这些表上应用了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命令来实现呢?

2 个答案:

答案 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个查询中的每一个进行分组。

答案 1 :(得分: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