Thursday, 12 September 2013

How to filter a table by a relation to another table in MYSQL, with a concatenate column?

How to filter a table by a relation to another table in MYSQL, with a
concatenate column?

Using MYSQL, I have 2 table : Contacts and Categories
+------+---------+
| id | title |
+------+---------+
| 10 | Cat |
| 11 | John |
| 12 | Sam |
+------+---------+
and the categories
+------+------------+----------+
| id | id_contact | category |
+------+------------+----------+
| 1 | 10 | C# |
| 2 | 10 | WPF |
| 3 | 11 | PHP |
| 4 | 11 | JQuery |
| 5 | 12 | MySQL |
| 6 | 12 | MSSQL |
| 7 | 12 | PHP |
+------+------------+----------+
I want to select for all the contacts that have a category of PHP in this
case the selection result would be :
+------+---------+---------------------+
| id | title | categories |
+------+---------+---------------------+
| 11 | John | PHP , JQuery |
| 12 | Sam | MySQL , MSSQL , PHP |
+------+---------+---------------------+
with a column that contains all the categories concatenated and separated
by "," . please advice.

No comments:

Post a Comment