我如何获得SQL中的金额范围?

时间:2014-01-29 18:35:57

标签: mysql sql

这是我的表格的样子

id | amount |     Consider table name as Amount
--------------
1  | 100
2  | 100
3  | 100
4  | 200
5  | 300
6  | 300

我想写一个查询,它返回我的范围计数:

amount | count
---------------
100    | 3
200    | 1
300    | 2

请帮帮我,我被困了!

EDIT1:我想输入金额范围!对不起,我错过了!

EDIT2:我可以传递多个范围,例如100到200,200到300,只有100

4 个答案:

答案 0 :(得分:1)

SELECT amount,COUNT(amount)as count FROM table 
GROUP BY amount
HAVING amount BETWEEN x AND y

如果您希望使用HAVING的特定金额的群组,如果您希望使用WHERE amount个别行...

答案 1 :(得分:1)

试试这个:

select amount,count(*)
from MyTable
where amount between 100 and 200
group by amount
order by amount

答案 2 :(得分:0)

使用此查询:

SELECT count(id) 
FROM Amount 
GROUP BY amount;

答案 3 :(得分:0)

如果我理解正确,你想要这样的范围吗?

SELECT amount,COUNT(amount)as count
FROM table 
WHERE amount > 5 AND amount < 600
GROUP BY amount