读书人

这个分组查询如何写

发布时间: 2012-03-14 12:01:12 作者: rapoo

这个分组查询怎么写?
如题:table表字段:serialno,username,cellphone,accountid,groupid
根据groupid分组查询,我想得到每组的cellphone(手机号)数据,我sql这样写
“select count(cellphone) from mdao_member where accountid = ? group by groupid”得到是每组的手机个数,
可是我想得到cellphone的具体数据该怎么做?

[解决办法]

SQL code
select cellphone,count(1) from mdao_member where accountid = ? group by cellphone
[解决办法]
分开查询吧。
[解决办法]
order by groupid
[解决办法]
用STUFF函数
[解决办法]
SQL code
select a.groupid,STUFF((select ';'+cellphone from mdao_member where groupid=a.groupid for xml path('')),1,1,'') as cellphonefrom mdao_member as a where a.accountid = ? group by a.groupid
[解决办法]
select cellphone,count(*) from mdao_member where accountid = ? group by groupid

读书人网 >SQL Server

热点推荐