在MySql中按多列排序注释部分

时间:2017-12-08 23:11:14

标签: php mysql

我的网站上有评论部分,我希望订购: 1.如果用户已将评论固定在顶部我希望这些评论首先显示 2.然后我想要写博客的用户发表的评论 3.然后我想要用户阅读博客的评论 4.最后我想要其余的评论

我想通过以下方式订购每个部分(1-4):
1.基于评论的upvotes / downvotes得分以及收到的评论有多少答案(=评论活动) 2.评论发布的日期

所以结果应该是这样的:( Bob写博客,Alice正在阅读它)

------------------------------------------------
| pin | username | score | date comment posted |
------------------------------------------------
| 1   | Bob      | 2     | 2017-01-25 13:45:01 | // First the pins ordered by Bob, Alice, score, date
------------------------------------------------
| 1   | Bob      | 1     | 2017-06-12 18:42:54 |
------------------------------------------------
| 1   | Alice    | 20    | 2017-03-21 12:51:42 |
------------------------------------------------
| 1   | Jack     | 30    | 2015-02-27 12:58:32 | 
------------------------------------------------
| NULL| Bob      | 11    | 2016-11-21 10:41:35 | // Then Bob's (as writer of blog) comments ordered by score, date
------------------------------------------------
| NULL| Bob      | 11    | 2016-10-30 23:56:01 |
------------------------------------------------
| NULL| Bob      | 9     | 2017-12-24 12:00:00 |
------------------------------------------------
| NULL| Alice    | 30    | 2017-04-21 13:48:05 | // Then Alice's (as reader of blog) comments ordered by score, date
------------------------------------------------
| NULL| Alice    | 30    | 2016-12-01 15:37:12 |
------------------------------------------------
| NULL| Alice    | 7     | 2017-05-21 11:12:11 |
------------------------------------------------
| NULL| Jack     | 93    | 2012-12-21 00:00:00 | // Then all other comments ordered by score, date
------------------------------------------------
| NULL| Jill     | 92    | 2015-04-08 15:45:29 |
------------------------------------------------
| NULL| Andrew   | 92    | 2014-12-31 19:45:12 |
------------------------------------------------
| NULL| Jack     | 32    | 2017-01-12 23:12:57 |
------------------------------------------------
.
.
.

但我得到的结果是pin / NULL正确排序的结果...
但是 - 只要pin在ORDER BY中是第一个 - 无论ORDER BY的其余部分(即ORDER BY引脚,......)按顺序排序,然后是Bob,然后是Alice,然后是日期。 ..

所以上面的结果我看起来像这样:

------------------------------------------------
| pin | username | score | date comment posted |
------------------------------------------------
| 1   | Jack     | 30    | 2015-02-27 12:58:32 | 
------------------------------------------------
| 1   | Alice    | 20    | 2017-03-21 12:51:42 |
------------------------------------------------
| 1   | Bob      | 2     | 2017-01-25 13:45:01 |
------------------------------------------------
| 1   | Bob      | 1     | 2017-06-12 18:42:54 |
------------------------------------------------
| NULL| Jack     | 93    | 2012-12-21 00:00:00 |
------------------------------------------------
| NULL| Jill     | 92    | 2015-04-08 15:45:29 |
------------------------------------------------
| NULL| Andrew   | 92    | 2014-12-31 19:45:12 |
------------------------------------------------
| NULL| Jack     | 32    | 2017-01-12 23:12:57 |
------------------------------------------------
| NULL| Alice    | 30    | 2017-04-21 13:48:05 |
------------------------------------------------
| NULL| Alice    | 30    | 2016-12-01 15:37:12 |
------------------------------------------------
| NULL| Bob      | 11    | 2016-11-21 10:41:35 |
------------------------------------------------
| NULL| Bob      | 11    | 2016-10-30 23:56:01 |
------------------------------------------------
| NULL| Bob      | 9     | 2017-12-24 12:00:00 |
------------------------------------------------
| NULL| Alice    | 7     | 2017-05-21 11:12:11 |
------------------------------------------------
.
.
.

为什么?我该怎么做才能解决它?

如果您需要信息:

SELECT语句(未编辑用于复制/粘贴)

SELECT comment.id AS commentID , comment.date AS cDate , comment.comment AS cComment , comment.pin AS cPin , comment.randomString AS cRandomString , commentuser.username AS cuUsername , commentuser.randomString AS cuRandomString , ( SELECT COUNT(*) FROM commentvote AS commentvote WHERE commentvote.commentID = comment.id AND commentvote.voteUp = 1 ) AS cVoteUp , ( SELECT COUNT(*) FROM commentvote AS commentvote2 WHERE commentvote2.commentID = comment.id AND commentvote2.voteDown = 1 ) AS cVoteDown , ( ( SELECT COUNT(*) FROM commentvote AS commentvoteup WHERE commentvoteup.commentID = comment.id AND commentvoteup.voteUp = 1 ) - ( SELECT COUNT(*) FROM commentvote AS commentvotedown WHERE commentvotedown.commentID = comment.id AND commentvotedown.voteDown = 1 ) + ( SELECT COUNT(*) FROM comment AS comment2 WHERE comment2.commentID = comment.id ) ) AS score FROM comment AS comment LEFT JOIN userlogininfo AS commentuser ON commentuser.id = comment.userID LEFT JOIN blog AS blog ON blog.randomString = ? INNER JOIN userlogininfo AS viewer ON viewer.randomString = ? INNER JOIN userlogininfo AS author ON author.id = blog.userID WHERE comment.blogID = blog.id AND comment.commentID IS NULL ORDER BY CAST(comment.pin AS UNSIGNED) DESC , CAST(author.id AS UNSIGNED) DESC , CAST(viewer.id AS UNSIGNED) DESC , CAST(score AS UNSIGNED) DESC , CAST(comment.date AS UNSIGNED) DESC

SELECT语句(易读)

SELECT 
    comment.id AS commentID , 
    comment.date AS cDate , 
    comment.comment AS cComment , 
    comment.pin AS cPin , 
    comment.randomString AS cRandomString , 
    commentuser.username AS cuUsername , 
    commentuser.randomString AS cuRandomString , 

    ( 
    SELECT 
        COUNT(*) 
    FROM commentvote 
    AS commentvote 
    WHERE 
        commentvote.commentID = comment.id 
        AND commentvote.voteUp = 1 
    ) 
    AS cVoteUp , 

    ( 
    SELECT 
        COUNT(*) 
    FROM commentvote 
    AS commentvote2 
    WHERE 
        commentvote2.commentID = comment.id 
        AND commentvote2.voteDown = 1 
    ) 
    AS cVoteDown , 

    ( 
        ( 
        SELECT 
            COUNT(*) 
        FROM commentvote 
        AS commentvoteup 
        WHERE 
            commentvoteup.commentID = comment.id 
            AND commentvoteup.voteUp = 1 
        ) 
        - // minus 
        ( 
        SELECT 
            COUNT(*) 
        FROM commentvote 
        AS commentvotedown 
        WHERE 
            commentvotedown.commentID = comment.id 
            AND commentvotedown.voteDown = 1 
        ) 
        + // plus
        ( 
        SELECT 
            COUNT(*) 
        FROM comment 
        AS comment2 
        WHERE 
            comment2.commentID = comment.id 
        ) 
    ) 
    AS score 

FROM comment 
AS comment 

LEFT JOIN 
    userlogininfo 
AS commentuser 
ON 
    commentuser.id = comment.userID 

LEFT JOIN 
    blog 
AS blog 
ON 
    blog.randomString = ? 

INNER JOIN 
    userlogininfo 
AS viewer 
ON 
    viewer.randomString = ? 

INNER JOIN 
    userlogininfo 
AS author 
ON 
    author.id = blog.userID 

WHERE 
    comment.blogID = blog.id 
    AND comment.commentID IS NULL 

ORDER BY 
    CAST(comment.pin AS UNSIGNED) DESC , // first order by pin/not-pin
    CAST(author.id AS UNSIGNED) DESC , // then order each by author/not-author
    CAST(viewer.id AS UNSIGNED) DESC , // then order not-author by viewer/rest
    CAST(score AS UNSIGNED) DESC , // then order pin/author/viewer/rest by score
    CAST(comment.date AS UNSIGNED) DESC // then order pin/author/viewer/rest/score by date

userlogininfo表:

--------------------------------
| id | username | randomString |
--------------------------------
| 1  | Bob      | lkjsdf786dsf |
--------------------------------
| 2  | Jack     | 78dsauhkwhe7 |
--------------------------------
| 3  | Jill     | asd78687asyd |
--------------------------------
| 4  | Alice    | ua67asdsd87j |
--------------------------------
| 5  | Andrew   | sadf987dsf7s |
--------------------------------
.
.
.

博客表

------------------------------
| id | userID | randomString |
------------------------------
| 1  | 3      | sad86f7s8d67 |
------------------------------
| 2  | 1      | isuydf786iuh |
------------------------------
| 3  | 2      | 876sdfhgwegk |
------------------------------
.
.
.

评论表

---------------------------------------------------------------------------
| id | date | userID | blogID | commentID | pin | ... All info needed ... |
---------------------------------------------------------------------------
| 1  |<date>| 3      |  2     | NULL      | 1   |                         |
---------------------------------------------------------------------------
| 2  |<date>| 4      |  1     | 1         | NULL|                         | // is an answer to comment with id 1
---------------------------------------------------------------------------
.
.
.

commentvote表

-----------------------------------------------
| id | userID | commentID | voteUp | voteDown |
-----------------------------------------------
| 1  | 2      | 3         | 1      | NULL     |
-----------------------------------------------
| 2  | 4      | 3         | 1      | NULL     |
-----------------------------------------------
| 3  | 3      | 2         | NULL   | 1        |
-----------------------------------------------
| 4  | 2      | 2         | 1      | NULL     |
-----------------------------------------------
.
.
.

希望有人可以提供帮助,因为我很难过......先谢谢......

1 个答案:

答案 0 :(得分:1)

试试这个:

ORDER BY pin DESC, FIELD(username, 'alice', 'bob') DESC, score DESC, date DESC

您可以通过向您的votecount表添加一个自动生成的列来简单地进行查询,该表返回总数,如下所示:

ALTER TABLE commentvote ADD COLUMN votes int GENERATED ALWAYS AS (voteUp-voteDown)