简体   繁体   中英

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. How to make an SQL query so i can list all teams with their members.

T1: Teams table

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

T2: Members table

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

T3: Records table

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.

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.

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;
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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