gpt4 book ai didi

php - 获取每个记录组的最后一条记录

转载 作者:可可西里 更新时间:2023-11-01 08:55:12 25 4
gpt4 key购买 nike

我不知道如何写获取最后一条记录的SQL语法(根据最近的帖子,未回复)。

我的 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 | |
+-------------------+-----------------------+------+-----+---------+----------------+

基本上对于每个线程通知,它应该获取每个通知记录的最后一条记录并检查 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

最佳答案

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

请注意,这将过滤每个用户的最后一条通知是一条私有(private)消息,并且 reply_id 为零。

关于php - 获取每个记录组的最后一条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5942206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com