这个分组查询怎么写?

风中云 2012-02-08 02:46:05
如题:table表字段:serialno,username,cellphone,accountid,groupid
根据groupid分组查询,我想得到每组的cellphone(手机号)数据,我sql这样写
“select count(cellphone) from mdao_member where accountid = ? group by groupid”得到是每组的手机个数,
可是我想得到cellphone的具体数据该怎么做?
...全文
114 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoxiangqing 2012-02-09
  • 打赏
  • 举报
回复
select cellphone,count(*) from mdao_member where accountid = ? group by groupid
风中云 2012-02-09
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 gogodiy 的回复:]
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.group……
[/Quote]
不是很懂,没用过这个stuff函数,报错了,说xml附近有错误
gogodiy 2012-02-08
  • 打赏
  • 举报
回复

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
gogodiy 2012-02-08
  • 打赏
  • 举报
回复
用STUFF函数
moleny 2012-02-08
  • 打赏
  • 举报
回复
order by groupid
风中云 2012-02-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 acherat 的回复:]
select groupid,cellphone
from tb
order by groupid
[/Quote]
汗,分组查啊
风中云 2012-02-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 rov_88 的回复:]
SQL code

select cellphone,count(1) from mdao_member where accountid = ? group by cellphone
[/Quote]
要按groupid分组查询的,如果按cellphone那不是每个号码都是一个组了
风中云 2012-02-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 acherat 的回复:]
select groupid,cellphone
from tb
order by groupid
[/Quote]
汗,分组查询啊
AAACCCEEEFFFA 2012-02-08
  • 打赏
  • 举报
回复
分开查询吧。
AcHerat 2012-02-08
  • 打赏
  • 举报
回复
select groupid,cellphone
from tb
order by groupid
rov_88 2012-02-08
  • 打赏
  • 举报
回复
select cellphone,count(1) from mdao_member where accountid = ? group by cellphone

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧