简体   繁体   English

在三个表的查询中。 如何在 SQL 中获得一组团队及其成员

[英]In a query of three tables. How to get an array of teams with their members in SQL

I have 3 tables: Teams, Members & Records.我有 3 个表:团队、成员和记录。 How to make an SQL query so i can list all teams with their members.如何进行 SQL 查询,以便我可以列出所有团队及其成员。

T1: Teams table T1:团队

id_team      name
------------------------
   1           Green team
   2           Blue Team

T2: Members table T2:成员

id_member      name
------------------------
   1           John  
   2           Lola
   3           Nancy
   4           Peter

T3: Records table T3:记录

id_record      id_team    id_member
-----------------------------------
   1             2           3
   2             1           2
   3             1           3
   4             2           4

This is what I have for now.这就是我现在所拥有的。

$teams = lib::$db->GetAll("SELECT SQL_CALC_FOUND_ROWS
                            t.*,
                            t.name AS team_name
                          FROM teams AS t
                            LEFT JOIN records r ON t.id_team = r.id_team
                            LEFT JOIN members m ON r.id_members  = m.id_member
                         ");

I need to list all teams with their members.我需要列出所有团队及其成员。 What I have above is listing all members that have a team.我上面列出的是所有拥有团队的成员。

Something like this:像这样的东西:

Green team has Lola & Nancy as members.绿色团队有 Lola 和 Nancy 作为成员。

Blue team has John & Peter as members.蓝队有约翰和彼得作为成员。

Select t.name, m.name
FROM records as r
  LEFT JOIN teams t ON t.id_team = r.id_team
  LEFT JOIN members m ON m.id_member == r.id_member

Also keep in mind that generally you should name tables after singular nouns not plural nouns and the primary key id columns could be named better (ie just id) since its already assumed.还要记住,通常你应该在单数名词而不是复数名词之后命名表,并且主键 id 列可以更好地命名(即只是 id),因为它已经假设了。

The above solution will not show teams that do not have any records since its a left join on the records table.上述解决方案不会显示没有任何记录的团队,因为它在记录表上是左连接。 If you wanted to see teams/members that do not have records you would need to do a full join.如果您想查看没有记录的团队/成员,您需要完全加入。

SELECT T.NAME, M.NAME 
FROM TEAMS T JOIN RECORDS R ON T.ID_TEAM=R.ID_TEAM 
JOIN MEMBERS M ON R.ID_MEMBER=M.ID_MEMBER

Try this it will works.................试试这个它会工作........

So I came up with this so far and it works.所以到目前为止我想出了这个并且它有效。 Please comment if syntax is OK or improve if possible.请评论语法是否正常或尽可能改进。

$teams = lib::$db->GetAll("SELECT
                                   t.*,
                                   t.name AS team_name
                               FROM teams t 
                               GROUP BY t.id_team");

if (!empty($teams )) {
   foreach($teams as &$team) {

    $teamId= (int) $team['id_team'];

     $teamMember = lib::$db->GetAssoc("SELECT
                                           r.*,
                                         GROUP_CONCAT(m.name) AS member_name
                                         FROM records AS r
                                         LEFT JOIN members AS m ON r.id_member = m.id_member
                                         WHERE id_team = $teamId
                                         GROUP BY r.id_record");
     $team['records'] = $teamMember;
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM