如何在mysql中获取最大值的相应数据

时间:2013-05-18 08:30:20

标签: mysql

我有一个查询A,结果是:

orders    |   date_added 

10        |     2013-01-09        
24        |     2013-01-10          
13        |     2013-01-11        

我希望获得对应日期的最大订单数量。 这是我到目前为止的查询

 SELECT MAX( orders ) 
 FROM (
         SELECT COUNT( order_id ) AS orders, DATE( date_added ) FROM  `order` 
         WHERE YEAR(date_added) = YEAR( NOW( ) ) 
         GROUP BY DATE( date_added )
        ) AS daily_orders"

1 个答案:

答案 0 :(得分:1)

我认为这会解决这个问题

SELECT COUNT( order_id ) AS orders, DATE( date_added ) FROM  `order` 
         WHERE YEAR(date_added) = YEAR( NOW( ) ) 
         GROUP BY DATE( date_added )
ORDER BY orders DESC LIMIT 1