简体   繁体   English

获取每个记录组的最后记录

[英]Get last record of each record group

I don't know how to write the SQL syntax of getting the last record (according to recent post and is not replied to). 我不知道如何编写获取最后一条记录的SQL语法(根据最近的帖子而没有回复)。

My table 我的桌子

+-------------------+-----------------------+------+-----+---------+----------------+
| Field             | Type                  | Null | Key | Default | Extra          |
+-------------------+-----------------------+------+-----+---------+----------------+
| notification_id   | mediumint(8) unsigned | NO   | PRI | NULL    | auto_increment |
| user_id           | mediumint(8) unsigned | NO   |     | NULL    |                |
| notification_msg  | text                  | NO   |     | NULL    |                |
| notification_date | int(11) unsigned      | NO   |     | NULL    |                |
| private_message   | tinyint(1) unsigned   | NO   |     | 0       |                |
| has_replied       | tinyint(1) unsigned   | NO   |     | 0       |                |
| reply_id          | mediumint(8) unsigned | NO   |     | 0       |                |
+-------------------+-----------------------+------+-----+---------+----------------+

Basically for each threaded notification, it should get the last record of each notification record and check if has_replied is 0 , if it is 0 then it should return it so PHP can read whether there is a notification that hasn't been replied to. 基本上对于每个线程通知,它应该获取每个通知记录的最后一条记录,并检查has_replied是否为0 ,如果它是0则它应该返回它,以便PHP可以读取是否有通知未被回复。 So like, it should return like this (pseudo): 所以,它应该像这样返回(伪):

+--------------+-----+-----+
| username     | 1   | 4   |
| username2    | 0   | 2   |
+--------------+-----+-----+

Where the second column represents if the last post has been replied to or not. 第二列表示最后一个帖子是否已被回复。

My current SQL syntax (works but does not get the last record and if it's replied to): 我当前的SQL语法(有效,但没有得到最后一条记录,如果它被回复):

SELECT n.*,
       m.user_id,
       m.username
FROM notifications n
INNER JOIN members m ON n.user_id = m.user_id
WHERE private_message = 1
AND reply_id = 0
ORDER BY has_replied ASC,
         notification_date DESC
Select m.user_id, m.username
    , N...
From members As M
    Join    (
            Select user_id, Max( notification_id ) As notification_id
            From notifications 
            Group By user_id
            ) As UserLastNotification
        On UserLastNotification.user_id = m.user_id
    Join notifications As N
        On N.notification_id = UserLastNotification.notification_id
Where N.private_message = 1
    And N.reply_id = 0
Order By N.has_replied, N.notification_date Desc

Note that this will filter on each user's last notification being a private message and having a reply_id being zero. 请注意,这将过滤每个用户的最后一个通知是私人消息并且reply_id为零。

A simple 一个简单的

LIMIT 1

at the end of the query should be sufficient to only return the last post. 在查询结束时应该只返回最后一篇文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM