我有两个表,一个是post表,另一个是comment table.in我使用post_id作为foreign_key 。 我现在想要的,一篇包含所有评论的帖子。 并且响应应该采用这种格式。 p>
{
“posid”:1,
“post_name”:“testpost”,
“注释”:[{\ n“comment_id”:1,
“comment_des”:“testcoment”
},{
“comment_id”:2,
“comment_des”:“testcoment2”
}
]
}
<\ < / code> pre>
对于这种类型的响应,任何人都可以为我编写简单的SQL查询吗? p>
我在codeigniter中尝试了以下查询,但是这次返回 多个结果,意味着一个帖子两次,因为一个帖子包含两个评论。 p>
$ this-&gt; db-&gt; select(“p.post_id,p.post_desc, p.post_time,c.id,c.comment_desc,c.comment_time“);
$ this-&gt; db-&gt; join(”asoc_comments as c“,”p.post_id = c.post_id“,”INNER“ );
$ response = $ this-&gt; db-&gt; get(“asgn_posts as p”) - &gt; result();
code> pre>
div>