这个分组查询如何写
这个分组查询怎么写?
如题:table表字段:serialno,username,cellphone,accountid,groupid
根据groupid分组查询,我想得到每组的cellphone(手机号)数据,我sql这样写
“select count(cellphone) from mdao_member where accountid = ? group by groupid”得到是每组的手机个数,
可是我想得到cellphone的具体数据该怎么做?
------解决方案--------------------
如题: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 cellphone from mdao_member as a where a.accountid = ? group by a.groupid
------解决方案--------------------
select cellphone,count(*) from mdao_member where accountid = ? group by groupid