查询计数帮助

时间:2014-03-11 02:47:05

标签: sql

我的查询并不完全满足我的需求:

select COUNT(distinct OrderID)
from Images
where OrderID like 'btm%'
group by OrderID

表格如

订单ID

btm1
btm1
btm1
btm2
btm2
btm3

我想回来:

btm1 | 3
btm2 | 2
btm3 | 1

2 个答案:

答案 0 :(得分:2)

select orderid, count(*) from Images group by orderid

答案 1 :(得分:2)

除了@Zdravko Danev所拥有的内容之外,如果你的查询确实需要,你可以添加like子句:

select orderid,COUNT(orderid)
from t1
where orderid like 'btm%'
group by orderid

fiddle