[英]Mysql select order by date desc then group by column
Hi i want to select from a table order by date desc than gruoup by smth. 嗨,我想按日期顺序从表顺序中选择,而不是按smuth进行组合。 At first phpmyadmin give an error (sql_mode=only_full_group_by) and i fix it by removing that flag from my.cnf and i don't know if this fix is good for long term, i read somewhere that with this fix you can get unwanted results.
起初phpmyadmin给出一个错误(sql_mode = only_full_group_by),我通过从my.cnf中删除该标志来修复它,而且我不知道此修复程序是否长期有效,我在某处读到,使用此修复程序,您可能会得到不想要的结果。 i've tried
**select * from ( select * from log order by date desc) tmp group by imei**
but the results are from the oldest date not from the newest date, it gives me the imei from the first id not from the last. 我已经尝试过
**select * from ( select * from log order by date desc) tmp group by imei**
但是结果是从最早的日期而不是最新的日期开始的,它使我从第一个id获得的imei不是最后。
For the grouping result, it doesn't matter if you've ordered the input. 对于分组结果,是否已订购输入都无关紧要。 If I interpret your question correctly, you want to get the latest log entry for each imei.
如果我正确解释了您的问题,那么您想获取每个imei的最新日志条目。 You would have to do something like this:
您将必须执行以下操作:
SELECT l1.*
FROM logs as l1
LEFT OUTER JOIN logs as l2
ON l1.imei = l2.imei
AND l1.date < l2.date
WHERE l2.date IS NULL
where you are essentially asking for each log entry (l1) for which no log entry (l2) exists with the same imei, but a greater date. 在这里,您实际上是在询问每个日志条目(l1),这些日志条目(l2)不存在具有相同imei但日期更长的日志条目。
If you only want to know when the last log entry for each imei was, this will be enough: 如果您只想知道每个imei的最后一个日志条目是何时,那么就足够了:
SELECT imei, MAX(date)
FROM log
GROUP BY imei
SELECT * FROM日志WHERE ID in(imei的SELECT max(id)FROM日志组)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.