选择MySQL表中每个值的最大值

时间:2011-12-21 05:15:26

标签: mysql sql

我有一个MySQL表,其中包含STATE和ORDERAMOUNTs列表:

State | OrderAmount
===================
New York  5
New York  6
Pennsylvania  6
Pennsylvania  7

我想选择所有状态的列表,其中包含任何单个订单LARGER,然后是给定金额,比如15,以及该状态的最大订单金额。如何使用单个查询迭代所有STtate值以获取此列表?

2 个答案:

答案 0 :(得分:3)

未经测试,但以下情况应该有效:

SELECT state, max(orderamount) AS amount FROM table GROUP BY state HAVING amount > 15;

答案 1 :(得分:0)

尝试类似:

Select s1.state, s1.orderamount, MAX(s2.orderamount) AS max_amount FROM states s1 
LEFT JOIN states s2 ON s1.id = s2.id
WHERE s1.orderamount > 15 GROUP BY s1.id