2016年8月12日金曜日

MySQL テーブル内、同一アイテムの最大値を求める

item no date
------ ------- -------
バナナ 2 8/1
バナナ 15 8/5
りんご 3 8/9
りんご 10 8/15
バナナ 20 8/21
梨 3 8/26
梨 5 9/10


SELECT max(x.no) FROM `table`

SELECT t.item,t.no,t.date
FROM `table` t
WHERE t.no = (
SELECT max(x.no) FROM `table` x WHERE x.item = t.item GROUP BY t.item
)