删除联接表中的重复项

时间:2018-09-18 19:25:55

标签: mysql sql join

我有4张这样的桌子: 项目

shell_exec

PMJOIN

1) UtilTest::testGetEnvironment
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'localhost'
+'gitlab'

重新加入

ID - TITLE - PMJOIN_ID - RESPJOIN_ID
1 - TEST - 1 - 2

用户

ID - PROJECTS_ID - USER_ID
1 -1 - 1 
1 -1 - 2 

我想要一个在一行中列出所有项目和负责人的所有项目的查询。将pms和负责人与(,)串联。

因此所需的输出将是:

ID - PROJECTS_ID - USER_ID
2 -1 - 2 

我尝试过的所有联接都提供重复的行。

1 个答案:

答案 0 :(得分:0)

Use aggregation functions like GROUP_CONCAT, with appropriate Joins.

Check the following query:

SELECT p.ID, 
       p.TITLE, 
       GROUP_CONCAT(DISTINCT u1.ALIAS) AS PMs, 
       GROUP_CONCAT(DISTINCT u2.ALIAS) AS RESPs
FROM PROJECTS AS p 
JOIN PMJOIN AS pmj ON pmj.PROJECTS_ID = p.ID 
JOIN USERS AS u1 ON u1.ID = pmj.USER_ID 
JOIN RESPJOIN AS rj ON rj.PROJECTS_ID = p.ID 
JOIN USERS AS u2 ON u2.ID = rj.USER_ID 
GROUP BY p.ID, p.TITLE