Question

How can I get the most recent date using MySQL? I tried max but I did not get the result that I want. My table looks like this:

+---------+---------+-----------------------+--------+------------+---------+
| Name    | idStore | Name                  | idItem | date       | price   |
+---------+---------+-----------------------+--------+------------+---------+
| walmart |       1 | Red Delicious Apples  |      1 | 2011-10-22 | 0.98000 |
| walmart |       1 | Red Delicious Apples  |      1 | 2011-10-28 | 0.98000 |
| walmart |       1 | Red Delicious Apples  |      1 | 2011-10-28 | 0.98000 |
| walmart |       1 | Red Delicious Apples  |      1 | 2011-11-22 | 0.98000 |
| walmart |       1 | Honeycrisp Apples     |      2 | 2011-10-22 | 1.98000 |
| walmart |       1 | Sonya Apples          |      3 | 2011-10-22 | 2.88000 |
| walmart |       1 | Gold Delicious Apples |      4 | 2011-10-22 | 0.98000 |
| walmart |       1 | Sweet Tango Apples    |      5 | 2011-10-22 | 2.48000 |
| walmart |       1 | Granny Smith Apples   |      6 | 2011-10-22 | 1.28000 |
| walmart |       1 | Fugi Apples           |      7 | 2011-10-22 | 1.38000 |
+---------+---------+-----------------------+--------+------------+---------+

I want to get this table:

+---------+---------+-----------------------+--------+------------+---------+
| Name    | idStore | Name                  | idItem | date       | price   |
+---------+---------+-----------------------+--------+------------+---------+
| walmart |       1 | Red Delicious Apples  |      1 | 2011-11-22 | 0.98000 |
| walmart |       1 | Honeycrisp Apples     |      2 | 2011-10-22 | 1.98000 |
| walmart |       1 | Sonya Apples          |      3 | 2011-10-22 | 2.88000 |
| walmart |       1 | Gold Delicious Apples |      4 | 2011-10-22 | 0.98000 |
| walmart |       1 | Sweet Tango Apples    |      5 | 2011-10-22 | 2.48000 |
| walmart |       1 | Granny Smith Apples   |      6 | 2011-10-22 | 1.28000 |
| walmart |       1 | Fugi Apples           |      7 | 2011-10-22 | 1.38000 |
+---------+---------+-----------------------+--------+------------+---------+

I am having hard time figuring this out. Thanks!

Was it helpful?

Solution

Online Example Query

https://data.stackexchange.com/stackoverflow/q/118881/how-can-i-get-the-most-resent-date-using-mysql

SELECT NameStore, idStore, Name, idItem, max(date) AS date, price
FROM tablename
GROUP by NameStore, idStore, Name, idItem, price
ORDER BY date DESC, idItem ASC

OTHER TIPS

You can use group by:

select NameStore, idStore, Name, idItem, max(date) date, price
from table
group by NameStore, idStore, Name, idItem, price

See SQL Select only rows with Max Value on a Column

SELECT yt1.*
FROM yourtable yt1
LEFT OUTER JOIN yourtable yt2 ON (yt1.idItem = yt2.idItem AND yt1.date < yt2.date)
WHERE yt2.idItem IS NULL;

put this at the end ORDER BY date DESC

So make it look like this:

SELECT * FROM `tablename` ORDER BY `date` DESC

Using DISTINCT(Name) or GROUP BY Name in the SQL sentence above, would make one item pop up only once.

You have to use the GROUP BY clause like this:

SELECT Name, idStore, Name, idItem, date, price 
FROM mytable 
GROUP BY idItem 
ORDER BY date DESC, idItem DESC;

Change the order direction using DESC or ASC. You can learn more about this reading the documentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top