如何使用 MySQL 获取最新日期?我尝试了 max 但没有得到我想要的结果。我的表看起来像这样:
+---------+---------+-----------------------+--------+------------+---------+
| 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 |
+---------+---------+-----------------------+--------+------------+---------+
我想得到这张表:
+---------+---------+-----------------------+--------+------------+---------+
| 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 |
+---------+---------+-----------------------+--------+------------+---------+
我很难弄明白这一点。谢谢!
最佳答案
您可以使用分组方式:
select NameStore, idStore, Name, idItem, max(date) date, price
from table
group by NameStore, idStore, Name, idItem, price
关于php - 如何使用 MySQL 获取最新日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8238869/