-4

I tried searching around but could not find the answer, because maybe of my keyword, but I am hoping someone could help. I have two tables like this :

+----+---------+
| ID | Parents |
+----+---------+
| 1  |  David  |
| 2  |  Peter  |
+----+---------+

The other table is:

+----+------------+------------+
| ID | Parents_id | Child_name |
+----+------------+------------+
| 1  |      1     |    Mike    |
| 2  |      1     |    John    |
| 3  |      2     |    Chris   |
+----+------------+------------+

Now on my browser, i wanted to have list something like this,

+---------+----------+
| Parents | Children |
+---------+----------+
| David   |  - Mike  |
|         |  - John  |
+---------+----------+
| Peter   |  Chris   |
+---------+----------+

Can use php or jquery to get it done.

Strawberry
  • 33,750
  • 13
  • 40
  • 57
i-faith
  • 449
  • 1
  • 3
  • 14
  • how is this related to jquery? – N. Ivanov Aug 24 '17 at 08:32
  • 2
    StackOverflow is here to help fix issues with code you've written, not provide free code. Please take some time to read the help page, especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – Rory McCrossan Aug 24 '17 at 08:32
  • https://stackoverflow.com/questions/13451605/how-to-use-group-concat-in-a-concat-in-mysql – Shafiqul Islam Aug 24 '17 at 08:37
  • https://www.percona.com/blog/2006/09/04/group_concat-useful-group-by-extension/ – Shafiqul Islam Aug 24 '17 at 08:37
  • 1
    The "keywords" you are looking for are "mysql tutorial join tables". You will find plenty of example code. Try out the code, update your question with the **code** you are having trouble with. –  Aug 24 '17 at 08:37

1 Answers1

2

You can get comma separated values from the query and format it in the browser, e.g.:

SELECT t1.parents, 
  (SELECT GROUP_CONCAT(child_name) FROM table2 WHERE parents_id = t1.ID GROUP BY parents_id) AS Children
FROM table1 t1;
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102