简体   繁体   中英

mysql group rows in joined query

Im stumbling upon a problem where i need to retrieve data from the following tables


events
+-------+---------+---------+
| e_id  | e_title | e_link  |
+-------+---------+---------+
|   1   | Event 1 | event_1 |
|   2   | Event 2 | event_2 |
|   3   | Event 3 | event_3 |
|   4   | Event 4 | event_4 |
|   5   | Event 5 | event_5 |
+-------+---------+---------+

reservations
+-------+---------+---------+
| r_id  | r_e_id  | r_u_id  |
+-------+---------+---------+
|   1   |    2    |    1    |
|   2   |    2    |    3    |
|   3   |    5    |    4    |
|   4   |    2    |    4    |
|   5   |    1    |    1    |
+-------+---------+---------+

users
+-------+---------+----------+
| u_id  | u_name  | u_gender |
+-------+---------+----------+
|   1   |   One   |   Male   |
|   2   |   Two   |   Male   |
|   3   |  Three  |  Female  |
|   4   |  Four   |   Male   |
|   5   |  Five   |  Female  |
+-------+---------+----------+

I want to display an event page with the users that are subscribed to that event, like follows:


Event 2

Users: - One - Three - Four

I have the following query with the problem that this one only displays the first user (so in this case Four ), which makes sense because the mysql_fetch_assoc() is not in a while() loop.


    $result = mysql_query(" 
        SELECT events.e_title, reservations.*, users.u_name
        FROM events
        JOIN reservations
        ON events.e_id = reservations.r_e_id
        JOIN users
        ON reservations.r_u_id = users.u_id
        WHERE events.e_link = '".mysql_real_escape_string($_GET['link'])."'
    ");
    $show = mysql_fetch_assoc($result);

What should i change in my query to make it work the way i want?

EDIT: The solution from Teez works perfect, but wat if i want to attach more info, say for a link? My desired output is something like this:


Event 2

Users: - <a href="domain.com/user/1">User 1</a> Male - <a href="domain.com/user/3">User 3</a> Female - <a href="domain.com/user/4">User 4</a> Male

How am i going to achieve that? And eventually i even want to split the users by gender. So one list for females and one for males

SECOND EDIT:

I'm stunned with the result so far, but to complete it i want to sort the users by gender, like so:


Event 2

Users male: - <a href="domain.com/user/1">User 1</a> Male - <a href="domain.com/user/4">User 4</a> Male

Users female: - <a href="domain.com/user/3">User 3</a> Female

but how?

Best way will be first make a 2D array containing all events with respective users

Like below:

  while( $show = mysql_fetch_assoc($result))
    {
     $events[$show['e_id']][]=$show['u_name'];
     $uid[$show['e_id']][]=$show['u_id'];

    }  

Then loop arround above array for displaying :

foreach($events ad $key=>$users)
{
   echo "Event ".$key."<br>";
   echo "Users : <br>";
   foreach($users as $ukey=>$name)
   {
        echo " -<a href='domain.com/user/".$uid[$key][$ukey]."'>".$name."</a>;
   }

}

So with each call of mysql_fetch_assoc you want to have the event details and a list of usernames? In MySQL you can use GROUP_CONCAT for this purpose, although it is quite limited and error-prone. You should rather put mysql_fetch_assoc() in a loop to build an array of users. Anyway, here is the GROUP_CONCAT solution:

$result = mysql_query(" 
    SELECT events.e_title, GROUP_CONCAT(users.u_name) e_reservation_users
    FROM events
    JOIN reservations ON events.e_id = reservations.r_e_id
    JOIN users ON reservations.r_u_id = users.u_id
    WHERE events.e_link = '".mysql_real_escape_string($_GET['link'])."'
    GROUP BY 1
");
$show = mysql_fetch_assoc($result);

$show will then be
array('e_title' => '...', 'e_reservation_users' => '...,...,...') .

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