我有两张mysql数据库表,一张用于发帖,一张用于评论。
发布表
+----+-------+
| ID | texts |
+----+-------+
| 1 | abc |
| 2 | xyz |
+----+-------+
和评论表
+----+--------+-------+
| ID | postid | texts |
+----+--------+-------+
| 1 | 1 | abc1 |
| 2 | 1 | abc2 |
| 3 | 1 | abc3 |
| 4 | 2 | xyz1 |
| 5 | 2 | xyz2 |
+----+--------+-------+
现在,如何以最少的 mysql 查询请求获取帖子,这样输出就像,
$data = array(
0 => array(
ID => 1,
texts => abc,
comments => array(
0 => array(
ID => 1,
texts => abc1
)
1 => array(
ID => 2,
texts => abc2
)
2 => array(
ID => 3,
texts => abc3
)
)
)
1 => array(
ID => 2,
texts => xyz,
comments => array(
0 => array(
ID => 4,
texts => xyz1
)
1 => array(
ID => 5,
texts => xyz2
)
)
)
)