Mysql获取最新数量的项目

时间:2014-09-08 06:38:18

标签: mysql sql database greatest-n-per-group

我这里有价目表。我需要得到的是最新的项目数量。

这是我当前的查询。 SELECT item, price, max(date) FROM price;

以下是我查询的内容。

结果:

item    | max(price)  | max(date)
----------------------------------
ballpen |   300       | 2014-12-11

表格结构

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+  ID  +    Item   +   Code    +  Price    +   Date     +  
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+  1   +  Ballpen  +  itm001   +  300      + 2014-07-29 +
+-------------------------------------------------------+
+  2   +  Ballpen  +  itm001   +  701      + 2013-08-29 +   
+-------------------------------------------------------+
+  3   +  Ballpen  +  itm001   +  700      + 2014-12-11 +
+-------------------------------------------------------+
+  4   +  Pencil   +  itm002   +  250      + 2014-12-11 +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++

预期产出

item    | max(price)  | max(date)
----------------------------------
ballpen |   700       | 2014-12-11
Pencil  |   250       | 2014-12-11

任何帮助都会表示赞赏。

1 个答案:

答案 0 :(得分:1)

SELECT 
  item,
  price, 
  `date`
FROM price
ORDER BY `date` DESC 
LIMIT 1

SQLFiddle here,还请注意date是保留关键字。