获取每个记录组的最后一条记录
我不知道如何编写获取最后一条记录的SQL语法(根据最近的帖子并且没有回复)。
我的表
+-------------------+-----------------------+------+-----+---------+----------------+
| 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 | |
+-------------------+-----------------------+------+-----+---------+----------------+
基本上对于每个线程通知,它应该获取每个通知记录的最后一条记录并检查 has_replied
是否为 0
,如果为 0 那么它应该返回它,以便 PHP可以读取是否有未回复的通知。就像这样,它应该像这样返回(伪):
+--------------+-----+-----+
| username | 1 | 4 |
| username2 | 0 | 2 |
+--------------+-----+-----+
其中第二列表示最后一个帖子是否已回复。
我当前的 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
I don't know how to write the SQL syntax of getting the last record (according to recent post and is not replied to).
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. 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):
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请注意,这将过滤每个用户的最后一条通知,该通知是私人消息且reply_id为零。
Note that this will filter on each user's last notification being a private message and having a reply_id being zero.
一个简单内容应该足以仅返回最后一篇文章。
查询末尾的
A simple
at the end of the query should be sufficient to only return the last post.