Good Morning,
I’m after some help please 🙂
I have a MySQL database with the following tables: incidents
, users
and incident_users
.
My incidents
and users
table are defined through a many-to-many relationship using the incident_users
table.
So, when a user adds an incident, the incident_id and user_id are captured. It is possible for an incident_id to be associated with multiple user_id.
I’m looking to return a result like so:
incident_id | user_fullname |
---|---|
1 | John Smith |
2 | Jane Doe & John Smith |
3 | Robert Doe |
Where user_fullname is a column from the users
table and the grouping by incident_id
2
Answers
This will be slightly vague as you have not shared the full details of your tables. But the basic idea should be along the lines of :
See also: https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_group-concat
Assuming your
incident_users
table is made up of aincident_id
and auser_id
then its a simple case of using the
incident_id
from this table and JOINing it to theUser
tableIf you have a
firstname
andlastname
column but nofullname
column, you can build a concatenation of the 2 you have as part of the query like this