有多个行项目id,我想为每个行项目id返回数量最大的行。在Mysql中,最好的方法是什么?
Row completed_at lineitem_id quantity
1 2020-03-12 4453468635184 1
2 2020-03-06 4453468635184 2
3 2020-03-17 4480921108528 4
4 2020-03-25 4480921108528 2
5 2020-03-25 4481446608944 3
6 2020-03-17 4481446608944 5
使用mysql,您可以这样写:
SELECT *
FROM (
SELECT *
FROM your_table
ORDER BY lineitem_id, quantity DESC
LIMIT 9999999999999 -- for mariadb, support order in subquery
) AS your_alias
GROUP BY lineitem_id