日期:2014-05-18  浏览次数:20514 次

这个sql语句怎么写?要按相同的电话

id     telephone
1         3824125
2         3214214
3         3824125
4         5624123

要求
选出id,telephone   以电话相同为分组

就是要group   by     telephone   ,同时要选出id,telephone   ,count(*)

该怎么写?

------解决方案--------------------
Select Min(id) As id, telephone, count(*) As count From 表 Group By telephone
------解决方案--------------------
或者,看看這個是不是你要的

Select A.*, B.[count]
From 表 A
Inner Join
(Select telephone, count(*) As count From 表 Group By telephone) B
on A.telephone = B.telephone
------解决方案--------------------
select a.id, b.telephone, b.cnt
from tbl a,
(select count(*) as cnt, telephone from tbl group by telephone) b
where a.telephone = b.telephone
------解决方案--------------------
既然分组,为何还要id? 分组把相同的合并,这个id取谁的id?
------解决方案--------------------
慕白兄说的有理
------解决方案--------------------
看了你的ID.不想回答.