读书人

这个sql语句如何写?要按相同的电话

发布时间: 2011-12-30 23:30:45 作者: rapoo

这个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.不想回答.

读书人网 >asp.net

热点推荐