Concatenate results into a single field
Say I have the two tables listed below in my database.
Code:
Table #1: persons
+----------------+
| id | name |
+----+-----------+
| 23 | Oluf |
| 24 | Christian |
| 25 | Jenny |
+----+-----------+
Table #2: person_pets
+----------------------+
| person_id | name |
+-----------+----------+
| 23 | Fido |
| 23 | Garfield |
| 23 | Casper |
| 25 | Shadow |
+-----------+----------+
Now, how can I construct a query which yields the results like this,
where the persons' pets are listed in a single field, separated with
a comma (or any other chosen character). Is it possible?
Code:
+---------------------------------------+
| id | name | pet_names |
+---------------------------------------+
| 23 | Oluf | Fido,Garfield,Casper |
| 24 | Christian | |
| 25 | Jenny | Shadow |
+---------------------------------------+