I have two tables:
table_a: table_b
+----+------+ +----+---------+------+
| id | name | | id | relation| name |
+----+------+ ++++++---------+------+
| 1 | bob | | 1 | friend | chris|
| 2 | jim | | 1 | friend | jon |
| 3 | tom | | 1 | brother | matt |
+----+------+ | 2 | friend | sam |
| 2 | parent | ron |
+----+---------+------+
and I want to enter a query to output something like
+----+------+------------+---------+--------+
| id | name |friend | brother | parent |
+----+------+------------+---------+--------+
| 1 | bob | chris, john| matt | |
| 2 | jim | sam | | ron |
+----+------+------------+---------+--------+
So the id is the comman variable between the two tables,
the relation variables have preset values (either friend, brother, parent and maybe a couple other types), and there can be multiple table_b.name per relation per id.
Is this too complicated a task to do?
SQLFiddle Demo
in future, if you have any other relation other than
friend, brother, and parentand you don’t want to alter the query, you can use prepared statementSQLFiddle Demo