查询以在不同的表中排序相关数据

时间:2014-06-09 10:24:21

标签: php mysql sql

有两个表:用户,评论

结构是这样的:

user
-----
id
name

comments
--------
id
title
user_id
comments_id

请注意, comments_id是该评论记录的父ID ,这意味着,如果comments_id为null,则为POST,如果它不为null,则为REPLY。

所以我想选择它们并像这样订购

Post 1
 Reply 1 for Post 1
 Reply 2 for Post 1
Post2
...

这是我尝试过的codeigniter查询,我还需要用户表来获取用户名

$this->db->select('c.id as id, u.name as username, c.txtMsg as txtMsg, c.createDate as createDate, c.updateDate as updateDate, c.imageURL as imageURL, c.postID as postID');
        $this->db->from('comment as c, user as u');
        $this->db->where('u.id = c.userID');
        $this->db->order_by('c.id', $order_type);

如何构建查询以订购列表?谢谢你的帮助。

0 个答案:

没有答案