我的sql语句是这样的
select peerid,count(peerid) as cnt from peeridstat where left(peerid,4)= '1155 ' group by peerid
查询结果是
peerid cnt
X 3
XX 2
我就想问一下为什么我使用count计算相同peerid的个数为什么不直接输出5而是分别输出3和2呢
后面我有把sql写成这样
select peerid,sum(count(peerid) as cnt) from peeridstat where left(peerid,4)= '1155 ' group by peerid
就出错了。
希望大家帮我解决一下,在下感激不尽~!
谢谢!
------解决方案--------------------
因为你按peerid分组了,所以peerid不同的就会有不同的cnt.
如果想输出5的话
select count(peerid) as cnt from peeridstat where left(peerid,4)= '1155 '
------解决方案--------------------
select A.peerid,cnt =(select count(peerid) from peeridstat B where left(B.peerid,4)=left(A.peerid,4)) from peeridstat A where left(A.peerid,4) = '1155 '