0

I have 2 types of items, 1000118500 and 1000438300 and I would like to know how to bring only the last item by the newest date.

so I would have only 2 lines, the newest ones

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

could anyone help? I'm using MariaDB

GMB
  • 216,147
  • 25
  • 84
  • 135
Peter Parker
  • 91
  • 1
  • 10

4 Answers4

0

You can filter with a subquery to get the latest row per item:

select t.*
from mytable t
where created = (select max(t1.created) from mytable t1 where t1.item = t.item)

This query would take advantage of an index on (item, created_at).

GMB
  • 216,147
  • 25
  • 84
  • 135
0

Did you try the MAX function

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

or see this post : How to select id with max date group by category in PostgreSQL?

coytech
  • 193
  • 7
-1
SELECT * FROM [TABLE_NAME] ORDER BY created DESC LIMIT 1
AndreiXwe
  • 723
  • 4
  • 19
-1

Assuming that created is a DateTime column, select what you want and order it by the date in descending order, then limit the result to 1 row, that will be the row with the newest date

select item 
from tablename
where id = 1000438300
order by created desc
limit 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149