我试着四处寻找但找不到答案,因为可能是我的关键字,但我希望有人可以提供帮助。 我有两个这样的表:
+----+---------+
| ID | Parents |
+----+---------+
| 1 | David |
| 2 | Peter |
+----+---------+
另一张表是:
+----+------------+------------+
| ID | Parents_id | Child_name |
+----+------------+------------+
| 1 | 1 | Mike |
| 2 | 1 | John |
| 3 | 2 | Chris |
+----+------------+------------+
现在在我的浏览器上,我希望列出类似这样的内容,
+---------+----------+
| Parents | Children |
+---------+----------+
| David | - Mike |
| | - John |
+---------+----------+
| Peter | Chris |
+---------+----------+
可以使用php或jquery来完成它。
答案 0 :(得分:2)
您可以从查询中获取逗号分隔值,并在浏览器中对其进行格式化,例如:
SELECT t1.parents,
(SELECT GROUP_CONCAT(child_name) FROM table2 WHERE parents_id = t1.ID GROUP BY parents_id) AS Children
FROM table1 t1;