几个表的mySQL JOIN

时间:2017-05-20 11:54:34

标签: mysql sql inner-join group-concat

我在MySQL-DB(InnoDB)中有以下三个表

UserTab
ID | Name  | ---
------------------
 1 | Tom   |
 2 | Dick  |
 3 | Harry |

EventTab
ID | Name      | ---
------------------
 1 | Easter    |
 2 | Holidays  |
 3 | ThxGiving |
 4 | Christmas |

ParticipationTab
ID | UserID | EventID
---------------------
 1 | 1      | 1
 2 | 1      | 2
 3 | 1      | 3
 4 | 2      | 1
 5 | 2      | 4
 6 | 3      | 3  

我希望通过查询获得以下结果:

QueryResultTab
UserTab.Name | EventTab.Name | NoPart | Names
-----------------------------------------------
 Tom         | Easter        | 2      | Tom, Dick
 Tom         | Holidays      | 1      | Tom
 Tom         | ThxGiving     | 2      | Tom, Harry
 Dick        | Easter        | 2      | Tom, Dick 
 Dick        | Christmas     | 1      | Dick
 Harry       | ThxGiving     | 2      | Tom, Harry

我知道Count()与GROUP结合以获得参与者数量 我知道group-concat获得“名字”。

SELECT Event, GROUP_CONCAT(Name ORDER BY Name ASC SEPARATOR ', ') as Names  
FROM  
(SELECT ID as UserID, Name FROM X_Users WHERE ConditionA) AS UserTab  
INNER JOIN  
(SELECT EventID, UserID FROM X_Participation WHERE ConditionB) AS ParticipationTab  
ON UserTab.UserID = ParticipationTab.UserID  
INNER JOIN  
(SELECT ID as EventID, Event FROM X_Events WHERE ConditionC) AS EventTab  
ON ParticipationTab.EventID = EventTab.EventID  
GROUP BY EventTab.EventID

这给了我:

ConcatTab
EventTab.Name | Names  
---------------------------  
Easter        | Tom, Dick  
Holidays      | Tom  
ThxGiving     | Tom, Harry  
Easter        | Tom, Dick  
Christmas     | Dick  
ThxGiving     | Tom, Harry  

我知道JOINs,你可以看到。可能我也可以使用LEFT或RIGHT JOIN。

对于其他部分,我使用此查询:

SELECT Name, Event, NoPart   
FROM (SELECT ID as UserID, Name FROM X_Users WHERE ConditionA) AS UserTab    
INNER JOIN (SELECT EventID, UserID FROM X_Participation WHERE ConditionB) AS PartTab   
ON UserTab.UserID = PartTab.UserID  
INNER JOIN (SELECT ID as EventID, Event FROM X_Events WHERE ConditionC) AS EvTab  
ON PartTab.EventID = EvTab.EventID  
INNER JOIN (SELECT EventID as CntID, COUNT(*) AS NoPart FROM X_Participation WHERE ConditionB) AS CntTab    
ON EvTab.EventID = CntTab.CntID  
ORDER BY UserTab.UserID

这给了我:

CountTab
UserTab.Name | EventTab.Name | NoPart 
--------------------------------------
 Tom         | Easter        | 2      
 Tom         | Holidays      | 1      
 Tom         | ThxGiving     | 2      
 Dick        | Easter        | 2      
 Dick        | Christmas     | 1      
 Harry       | ThxGiving     | 2      

但是如何将ConcatTab和CountTab合并/合并到QueryResultTab?我想用mysql_fetch_assco()逐行检索PHP中的结果表。   请不要告诉我有关PDO等的信息。我知道。

另一个选项 - 我试图避免的 - 是在PHP循环中执行它并使用许多小的SQL查询来实现结果。

1 个答案:

答案 0 :(得分:0)

根据您的示例数据,您需要参与表中的所有行以及来自维度的信息。然后,您需要该表的摘要。

这是一种在FROM子句中使用子查询的方法:

SELECT u.name, e.name, p2.numPart, p2.Names
FROM X_Participation p INNER JOIN  
     X_Users u 
     ON u.UserID = p.UserID INNER JOIN  
     X_Events e
     ON p.EventID = e.EventID INNER JOIN
     (SELECT p2.EventId, COUNT(*) as numPart,
             GROUP_CONCAT(u2.name SEPARATOR ', ') as names
      FROM X_Participation p2 INNER JOIN  
           X_Users u2 
           ON u2.UserID = p2.UserID
      GROUP BY p2.EventId
     ) p2
     ON p2.EventId = p.EventId;

注意:

  • 如果您要使用表别名(您应该使用),请将它们缩短,不要超过表名。
  • 不要不必要地使用子查询。在实现子查询的MySQL中尤其如此。
  • 您可以在外部查询的WHERE子句中添加其他条件。