按子项分组,但如果是新读取,如果不是 1,则显示 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它不起作用,因为无论您如何尝试对集合进行排序,mysql 都可以从组中返回任何行。要使用某种自定义顺序查找组中的第一行,您必须将其分为两个任务 - 首先查找分组所依据的列的所有不同值,然后查找子查询中每个引用值的第一行。因此,您的查询应如下所示:
确保您在 (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:
Make sure you have an index on (touserid,subkey). Depending on how big your db is you may need more.