按子项分组,但如果是新读取,如果不是 1,则显示 0

发布于 2024-11-05 19:13:20 字数 486 浏览 2 评论 0原文

好吧,我知道这听起来很愚蠢。但我已经尝试了一切。

这是我的代码,

SELECT toD.username AS ToUser,
fromD.username AS FromUser, 
rvw.* FROM usermessages AS rvw 

LEFT JOIN users AS toD 
ON toD.id = rvw.touserid 

LEFT JOIN users AS fromD ON fromD.id = rvw.fromuserid 

WHERE touserid = '" . $this->userid . "'
AND deleted = '0' 

GROUP BY subkey 

ORDER BY rvw.read ASC, rvw.created DESC

虽然这确实有效,但我发现如果有新消息,并且读取为 0,它仍然显示为 1。我知道这是因为我将行分组在一起。

但我不确定是否还有其他方法可以做到这一点。

Ok I know this is going to sound stupid. But I have tried everything.

Here is my code to start of with

SELECT toD.username AS ToUser,
fromD.username AS FromUser, 
rvw.* FROM usermessages AS rvw 

LEFT JOIN users AS toD 
ON toD.id = rvw.touserid 

LEFT JOIN users AS fromD ON fromD.id = rvw.fromuserid 

WHERE touserid = '" . $this->userid . "'
AND deleted = '0' 

GROUP BY subkey 

ORDER BY rvw.read ASC, rvw.created DESC

while this does work, what I am finding is that if there is a new message, and the read is 0 it still shows up as 1. I know this is because I am grouping the rows together.

But am not sure of any other way to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〗斷ホ乔殘χμё〖 2024-11-12 19:13:20

它不起作用,因为无论您如何尝试对集合进行排序,mysql 都可以从组中返回任何行。要使用某种自定义顺序查找组中的第一行,您必须将其分为两个任务 - 首先查找分组所依据的列的所有不同值,然后查找子查询中每个引用值的第一行。因此,您的查询应如下所示:

SELECT toD.username AS ToUser, fromD.username as FromUser, msg.* FROM 
       ( SELECT DISTINCT touserid, subkey
       FROM usermessages
       WHERE touserid = 'insert_your_id_here'
       AND deleted=0 ) msgg
JOIN usermessages msg
ON msg.id = ( SELECT msgu.id
              FROM usermessages msgu
              WHERE msgu.touserid = msgg.touserid
              AND msgu.subkey = msgg.subkey
              AND deleted=0
              ORDER BY msgu.read ASC, msgu.created DESC
              LIMIT 1 )
JOIN users fromD ON msg.fromuserid = fromD.id
JOIN users toD ON msg.touserid = toD.id

确保您在 (touserid,subkey) 上有索引。根据您的数据库有多大,您可能需要更多。

It doesn't work because mysql can return any row from the group no matter how you try to order your set. To find first row in the group using some custom order you have to split it into two tasks - first finding all distinct values for the column you group by and then finding first row in the subquery for every referenced value. So your query should look like:

SELECT toD.username AS ToUser, fromD.username as FromUser, msg.* FROM 
       ( SELECT DISTINCT touserid, subkey
       FROM usermessages
       WHERE touserid = 'insert_your_id_here'
       AND deleted=0 ) msgg
JOIN usermessages msg
ON msg.id = ( SELECT msgu.id
              FROM usermessages msgu
              WHERE msgu.touserid = msgg.touserid
              AND msgu.subkey = msgg.subkey
              AND deleted=0
              ORDER BY msgu.read ASC, msgu.created DESC
              LIMIT 1 )
JOIN users fromD ON msg.fromuserid = fromD.id
JOIN users toD ON msg.touserid = toD.id

Make sure you have an index on (touserid,subkey). Depending on how big your db is you may need more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文