[英]How can I get the most recent date and the Most frequently occurring values in MySQL database
如何获取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;
我很难解决这个问题。 谢谢!
您必须使用子查询,这是优化您的请求的最佳方法:
您第一次选择max(date),第二次再次选择价格
尝试这个
创建表#temp(从名称的tblname组中选择max(date),Name)
然后在#temp表上进行全表扫描,并从tblname中获取ID,其中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.