无法从mysql中选择最新的而不是相同的数据
16lz
2021-01-22
I have value like this.
我有这样的价值。
id website time
1 aaa.com 11.10
2 bbb.com 11.10
3 ccc.com 11.10
4 aaa.com 11.11
5 bbb.com 11.11
6 ccc.com 11.11
I want to select latest and not same data in table. It should be .
我想在表格中选择最新的而不是相同的数据。它应该是 。
4 aaa.com 11.11
5 bbb.com 11.11
6 ccc.com 11.11
I query like this
我这样查询
"SELECT * FROM name GROUP BY website ORDER BY id DESC";
It alway show DESC and ASC.
它总是显示DESC和ASC。
1 aaa.com 11.10
2 bbb.com 11.10
3 ccc.com 11.10
How to select latest value with not same data ?
如何选择不同数据的最新值?
1 个解决方案
#1
2
Join to a subquery which finds the latest record for each website:
加入子查询,查找每个网站的最新记录:
SELECT t1.*
FROM yourTable t1
INNER JOIN
(
SELECT website, MAX(time) AS max_time
FROM yourTable
GROUP BY website
) t2
ON t1.website = t2.website AND
t1.time = t2.max_time;
You are storing your times apparently in some text format. This isn't desirable, and it would be better to store date times instead. If the width of the time is always the same (5 telling from your sample data), then my query should work though.
您显然以某种文本格式存储您的时间。这是不可取的,而且最好存储日期时间。如果时间的宽度总是相同的(从样本数据中得知),那么我的查询应该可行。
更多相关文章
- PHPExcel导入导出及大量数据导入缓存方式的修改
- php-fpm通过request_slowlog_timeout检查哪个脚本执行时间长
- 无法从Ajax POST请求中将带空格的数据导入数据库
- php时间函数——获取过去24小时内每个小时的节点
- 将纪元时间转换为日期PHP
- php写app接口并返回json数据
- MySQL错误1046:未选择数据库
- 社交登录实现的数据库结构?
- Json调用JSON.parse:意外结束数据