按日期选择最后一项

selects the last item by date

我有 2 种物品,1000118500 和 1000438300,​​我想知道如何在最新日期前只带最后一件物品。

所以我只有 2 行,最新的

id item       created
1  1000438300 2018-10-26 00:43:30
2  1000438300 2018-10-16 00:44:02
3  1000118500 2018-10-13 00:41:27
4  1000438300 2018-09-26 00:47:28
5  1000118500 2018-08-09 00:40:09
6  1000438300 2018-07-12 12:05:37
7  1000118500 2018-06-28 20:06:24
8  1000118500 2018-06-28 20:06:07

有人能帮忙吗? 我正在使用 MariaDB

SELECT * FROM [TABLE_NAME] ORDER BY created DESC LIMIT 1

假设created是一个DateTime列,select你想要的按日期降序排列,然后将结果限制为1行,这将是带有最新日期

select item 
from tablename
where id = 1000438300
order by created desc
limit 1

您可以使用子查询进行过滤以获取每个 item:

的最新行
select t.*
from mytable t
where created = (select max(t1.created) from mytable t1 where t1.item = t.item)

此查询将利用 (item, created_at) 上的索引。

你试过MAX函数了吗

SELECT item, MAX(created)
    FROM table
GROUP BY item

或者看这个post:How to select id with max date group by category in PostgreSQL?