如何获取MySQL数据库中最新的日期和最常出现的值?
我有以下数据库:
+----+---------+---------+-----------------------+--------+------------+---------+
| id | Name | idStore | Name | idItem | date | price |
+----+---------+---------+-----------------------+--------+------------+---------+
| 1 | walmart | 1 | Red Delicious Apples | 1 | 2011-10-28 | 0.93000 |
| 2 | walmart | 1 | Red Delicious Apples | 1 | 2011-10-28 | 0.98000 |
| 3 | walmart | 1 | Red Delicious Apples | 1 | 2011-10-29 | 0.90000 |
| 4 | walmart | 1 | Red Delicious Apples | 1 | 2011-10-29 | 0.91000 |
| 5 | walmart | 1 | Red Delicious Apples | 1 | 2011-10-29 | 0.92000 |
| 6 | walmart | 1 | Red Delicious Apples | 1 | 2011-10-29 | 0.92000 |
| 7 | walmart | 1 | Red Delicious Apples | 1 | 2011-10-29 | 0.92000 |
| 8 | walmart | 1 | Red Delicious Apples | 1 | 2011-10-29 | 0.93000 |
| 9 | walmart | 1 | Red Delicious Apples | 1 | 2011-10-28 | 0.93000 |
| 10 | walmart | 1 | Red Delicious Apples | 1 | 2011-10-28 | 0.94000 |
| 11 | walmart | 1 | Red Delicious Apples | 1 | 2011-10-28 | 0.94000 |
| 12 | walmart | 1 | Red Delicious Apples | 1 | 2011-10-28 | 0.94000 |
| 13 | walmart | 1 | Red Delicious Apples | 1 | 2011-10-28 | 0.94000 |
| 14 | walmart | 1 | Red Delicious Apples | 1 | 2011-10-28 | 0.94000 |
| 15 | walmart | 1 | Red Delicious Apples | 1 | 2011-10-12 | 0.98000 |
| 16 | walmart | 1 | Honeycrisp Apples | 2 | 2011-10-22 | 1.98000 |
| 17 | walmart | 1 | Sonya Apples | 3 | 2011-10-22 | 2.88000 |
| 18 | walmart | 1 | Gold Delicious Apples | 4 | 2011-10-22 | 0.98000 |
| 19 | walmart | 1 | Sweet Tango Apples | 5 | 2011-10-22 | 2.48000 |
| 20 | walmart | 1 | Granny Smith Apples | 6 | 2011-10-22 | 1.28000 |
| 21 | walmart | 1 | Fugi Apples | 7 | 2011-10-22 | 1.38000 |
+----+---------+---------+-----------------------+--------+------------+---------+
我想得到以下结果:
+----+---------+---------+-----------------------+--------+------------+---------+
| id | Name | idStore | Name | idItem | date | price |
+----+---------+---------+-----------------------+--------+------------+---------+
| 5 | walmart | 1 | Red Delicious Apples | 1 | 2011-10-29 | 0.92000 |
| 16 | walmart | 1 | Honeycrisp Apples | 2 | 2011-10-22 | 1.98000 |
| 17 | walmart | 1 | Sonya Apples | 3 | 2011-10-22 | 2.88000 |
| 18 | walmart | 1 | Gold Delicious Apples | 4 | 2011-10-22 | 0.98000 |
| 19 | walmart | 1 | Sweet Tango Apples | 5 | 2011-10-22 | 2.48000 |
| 20 | walmart | 1 | Granny Smith Apples | 6 | 2011-10-22 | 1.28000 |
| 21 | walmart | 1 | Fugi Apples | 7 | 2011-10-22 | 1.38000 |
+----+---------+---------+-----------------------+--------+------------+---------+
我使用以下查询,但它不起作用,因为它为我提供了所有具有最新日期的项目
select id, store_name as Name, idStore, Name, idItem , max(Price.date), min(Price.price) From table group by idStore, Store.Name, idItem, Item.Name, price ORDER BY idItem ASC, date DESC;
我从分组中删除了价格并获得最低价格(不是最常发生的价格)
select id, store_name as Name, idStore, Name, idItem , max(Price.date), min(Price.price) From table group by idStore, Store.Name, idItem, Item.Name ORDER BY idItem ASC, date DESC;
我很难搞清楚这一点。谢谢!
答案 0 :(得分:0)
您必须使用子查询,这是优化您的请求的最佳方式:
您选择的第一个(日期)&第二个你再次过滤价格
答案 1 :(得分:0)
试试这个
创建表#temp ( 选择最大(日期),名称 来自tblname 按名称分组 )
然后在#temp表上进行全表扫描并从tblname获取id