mySQL选择条件replyTo

时间:2013-12-08 02:48:08

标签: php mysql

我知道如何检索每个帖子的评论,但现在我正在尝试自己的系统'回复'。当有人回复时,我会在新评论中将评论ID存储为 reply_id

$sql = 'SELECT cid, cname, user_uid, reply_id, 
        published, content, avatar
        FROM comments, users 
        WHERE report < 3 AND (uid = user_uid AND post_pid='.$id.') 
        OR ( user_uid ="_'.$id.'" AND post_pid='.$id.')';

此请求返回此评论列表:

    Array
    (
        [0] => stdClass Object
            (
                [cid] => 101
                [cname] => ramzan
                [user_uid] => 1
                [reply_id] => 100
                [published] => 2013-12-08 01:44:56
                [content] => why?
                [avatar] => users/anonyme.png
            )

        [1] => stdClass Object
...

但我想得到这样的东西:

Array
        (
            [0] => stdClass Object
                (
                    [cid] => 101
                    [cname] => ramzan
                    [user_uid] => 1
                    [reply_id] => array() // the reply if exist
                    [published] => 2013-12-08 01:44:56
                    [content] => why?
                    [avatar] => users/anonyme.png

                )
也许这样做是个不错的主意,但我不知道怎么做!!!

1 个答案:

答案 0 :(得分:0)

也许有人可以帮忙解决这个问题,我这样解决了我的问题: lvl1,lvl2必须声明为null,只有当有人回复评论时才设置字段值

...SELECT field FROM table where ...
   ORDER BY COALESCE (lvl1, lvl2, id ), lvl1, lvl2, id
...

谢谢,

祝你好运