I have two MySQL tables and want to join it correctly by one sql query.
Table "users"
id | |
---|---|
1 | [email protected] |
2 | [email protected] |
3 | [email protected] |
4 | [email protected] |
5 | [email protected] |
Table "group_user"
user_id | group_id |
---|---|
1 | 1 |
2 | 1 |
2 | 5 |
2 | 6 |
3 | 5 |
3 | 6 |
4 | 1 |
5 | 5 |
I want to have result:
id | group_ids | |
---|---|---|
1 | [email protected] | 1 |
2 | [email protected] | 1,5,6 |
3 | [email protected] | 5,6 |
4 | [email protected] | 1 |
5 | [email protected] | 5 |
I don’t understand how to do it correctly. Please help me write the correct query to MySQL.
2
Answers
This can be done using the aggregate function
GROUP_CONCAT()
:with postgres you can use this request:
source: https://www.postgresql.org/docs/current/functions-aggregate.html#FUNCTIONS-AGGREGATE-TABLE