在MySQL中按顺序排列有2个不同列的数据

时间:2013-09-27 06:05:18

标签: php mysql

我正在与讨论区合作,其中会有某个主题,用户可以评论或提出其他评论。每条评论也可以由其他用户回复,并会收到通知。

我的通知表

  • notification_id是将收到通知的登录用户的user_id
  • notification_from是评论或喜欢用户发帖的用户ID。
  • notification_topic是主题的ID加上通知类型,可区分是评论还是upvote
  • notification_comment是主题
  • 中的comment_id
+-----------------+-------------------------+-------------------+--------------------------------------+----------------------+-------------------+-------------------+--------------------+----------------------+---------+--------+
| notification_id | notification_pic        | notification_name | notification_title                   | notification_user_id | notification_date | notification_from | notification_topic | notification_comment | type    | status |
+-----------------+-------------------------+-------------------+--------------------------------------+----------------------+-------------------+-------------------+--------------------+----------------------+---------+--------+
|               1 | 32_1380182434_thumb.jpg | Sheena Salazar    | Chrysler files papers for share sale |                    2 | 1380188338        | 32                | 83_upvote          | 1                    | upvote  | read   |
|               2 | 32_1380182434_thumb.jpg | Sheena Salazar    | Chrysler files papers for share sale |                    2 | 1380188342        | 32                | 83_comment         | 1                    | comment | read   |
|               3 | 93_1379990163_thumb.jpg | vhon samson       | Chrysler files papers for share sale |                    2 | 1380188505        | 93                | 83_upvote          | 1                    | upvote  | read   |
|               4 | 93_1379990163_thumb.jpg | vhon samson       | Chrysler files papers for share sale |                    2 | 1380188509        | 93                | 83_comment         | 1                    | comment | read   |
|               5 | 93_1379990163_thumb.jpg | vhon samson       | Chrysler files papers for share sale |                    0 | 1380246975        | 93                | 83_comment         | 1                    | comment | unread |
|               6 | 93_1379990163_thumb.jpg | vhon samson       | Toyota and Nissan in vehicle recall  |                    2 | 1380247149        | 93                | 225_comment        | 3                    | comment | read   |
|               7 | default.gif             | kath aguilar      | Chrysler files papers for share sale |                    2 | 1380253584        | 7                 | 83_comment         | 1                    | comment | read   |
|               8 | default.gif             | kath aguilar      | Chrysler files papers for share sale |                   93 | 1380253870        | 7                 | 83_comment         | 2                    | comment | unread |
+-----------------+-------------------------+-------------------+--------------------------------------+----------------------+-------------------+-------------------+--------------------+----------------------+---------+--------+

我的查询

这是我查询和分组我的表格的方式:

SELECT *, COUNT(notification_topic) AS topic_count
    FROM tbl_notification
    WHERE notification_user_id =  '{$_SESSION['id']}'
    GROUP BY notification_topic
    ORDER BY notification_date DESC 
    LIMIT 8

这是查询的结果:

+-----------------+-------------------------+-------------------+--------------------------------------+----------------------+-------------------+-------------------+--------------------+----------------------+---------+--------+-------------+
| notification_id | notification_pic        | notification_name | notification_title                   | notification_user_id | notification_date | notification_from | notification_topic | notification_comment | type    | status | topic_count |
+-----------------+-------------------------+-------------------+--------------------------------------+----------------------+-------------------+-------------------+--------------------+----------------------+---------+--------+-------------+
|               6 | 93_1379990163_thumb.jpg | vhon samson       | Toyota and Nissan in vehicle recall  |                    2 | 1380247149        | 93                | 225_comment        | 3                    | comment | read   |           1 |
|               2 | 32_1380182434_thumb.jpg | Sheena Salazar    | Chrysler files papers for share sale |                    2 | 1380188342        | 32                | 83_comment         | 1                    | comment | read   |           3 |
|               1 | 32_1380182434_thumb.jpg | Sheena Salazar    | Chrysler files papers for share sale |                    2 | 1380188338        | 32                | 83_upvote          | 1                    | upvote  | read   |           2 |
+-----------------+-------------------------+-------------------+--------------------------------------+----------------------+-------------------+-------------------+--------------------+----------------------+---------+--------+-------------+

sqlfiddle上查看。

我需要什么

我需要计算一下,以便我可以在facebook上提出类似的通知:

enter image description here

我的主要问题是我ORDER之前notification_date无法GROUP BY因为我需要将每个notification_commentnotification_topic隔离开来。我希望显示评论或喜欢每个主题的最新用户。我该怎么做?

1 个答案:

答案 0 :(得分:1)

如果我已正确理解您的问题,那么您需要groupwise maximum,可以通过选择组中的识别条件然后加入表格来获取:

SELECT   *
FROM     tbl_notification NATURAL JOIN (
           SELECT   notification_topic,
                    notification_user_id,
                    MAX(notification_date) AS notification_date,
                    COUNT(*)               AS topic_count
           FROM     tbl_notification
           WHERE    notification_user_id = 2
           GROUP BY notification_topic,
                    notification_user_id
         ) AS t
ORDER BY notification_date DESC 
LIMIT    8

sqlfiddle上查看。