MySQL – Group_Concat with Joining Multiple Tables

group-concatenationjoin;MySQL

I have the following scheme

users:        researches:                  researchyear:

uid | name    rid| titleAr                  rid     |  uid     | 
---+--------  ---+------------              --------+----------+
 1 | Peter     1 | Tennis                         1 |        1 |
 2 | Alice     2 | Football                       1 |        2 | 
 3 | Bob       3 | Basketball                     2 |        3 | 
                                                  3 |        1 |
                                                  3 |        3 | 
                                                  3 |        2 |

I would like to get a research with its researchers (users) by specific researcher_Id(uid),
i.e when I set user id (1,3 or 2) I would like to get the same result.

id |titleAr    | users_ids | users_names
---+-------+-----------+----------------------------
 3 |Basketball | 1,3,2     | Peter,Bob,Alice

I've tried the following query where I can get every research with its researchers (users):

SELECT r.rId,
       r.titleAr,
       Group_concat(u.userId ) users_ids ,
      Group_concat(u.name )      users_names
FROM   research r
           LEFT JOIN researchyear ry
                     ON r.rId = ry.rId
           LEFT  JOIN users u
                      ON ry.uId = u.userId
GROUP  BY r.rId, r.titleAr

How can I get it for one user?

Best Answer

If you want to list all users names including the uid you want put this in your where clause:

WHERE EXISTS(SELECT * FROM researchyear t WHERE t.rid = r.id AND t.uid = {your_uid} )