我想得到count(*)
几个范围之间的总和。
例如,我select count(*)
从一个位置范围到另一个位置范围有 10 条语句。
我想得到所有 10 个位置范围的总和。
SELECT count (*)
FROM locn_hdr
WHERE locn_brcd
BETWEEN 'B2121' AND 'B2144'
SELECT count (*)
FROM locn_hdr
WHERE locn_brcd
BETWEEN 'B2201' AND 'B2224'
如果您想要单独的结果,您可以使用以下查询:
select sum(if(locn_brcd between 'B2121' and 'B2144',1,0)) as ca1,
sum(if(locn_brcd between 'B2201' and 'B2224',1,0)) as ca2,
sum(if(locn_brcd between 'B2322' and 'B2344',1,0)) as ca3,
sum(if(locn_brcd between 'B2401' and 'B2424',1,0)) as ca4,
sum(if(locn_brcd between 'B2521' and 'B2544',1,0)) as ca5,
sum(if(locn_brcd between 'B2601' and 'B2624',1,0)) as ca6,
sum(if(locn_brcd between 'B0317' and 'B0340',1,0)) as ca7,
sum(if(locn_brcd between 'B0401' and 'B0424',1,0)) as ca8,
sum(if(locn_brcd between 'B0517' and 'B0540',1,0)) as ca9,
sum(if(locn_brcd between 'B0601' and 'B0624',1,0)) as ca10
from locn_hd;
在 mysql 中试试这个查询:
select (sum(if(locn_brcd between 'B2121' and 'B2144',1,0))+sum(if(locn_brcd between 'B2201' and 'B2224',1,0))) as total from locn_hdr