50分,求 一 统计条数的 sql 语句!解决方法
50分,求 一 统计条数的 sql 语句!
如题,有如下 数据
姓名
张三
张三
张三
李四
李四
想得到的结果是
姓名 总数
张三 3
李四 2
------解决方案--------------------
如题,有如下 数据
姓名
张三
张三
张三
李四
李四
想得到的结果是
姓名 总数
张三 3
李四 2
------解决方案--------------------
- SQL code
select 姓名 , count(*) 总数 from tb group by 姓名
------解决方案--------------------
- SQL code
create table tb(姓名 varchar(10)) insert into tb values('张三') insert into tb values('张三') insert into tb values('张三') insert into tb values('李四') insert into tb values('李四') GO select 姓名 , count(*) 总数 from tb group by 姓名 drop table tb /* 姓名 总数 ---------- ----------- 李四 2 张三 3 (所影响的行数为 2 行) */
------解决方案--------------------
- SQL code
create table tb(姓名 varchar(10)) insert into tb values('张三') insert into tb values('张三') insert into tb values('张三') insert into tb values('李四') insert into tb values('李四') GO select distinct 姓名 , (select count(*) from tb where 姓名=a.姓名) 总数 from tb a drop table tb
------解决方案--------------------
上面都对的 帮顶
------解决方案--------------------
- SQL code
create table tb(username varchar(10)) insert into tb values('张三') insert into tb values('张三') insert into tb values('张三') insert into tb values('李四') insert into tb values('李四') GO select username,count(*) 总数 from tb group by username