表:
id t_name f_name1 植物 树
2 植物 花
3 植物 草
4 动物 猪
5 动物 狗
6 动物 牛
7 动物 羊
8 微生物 XXXX菌
9 微生物 AAAA菌其中id是自增加字段,现在想select达到这样的效果:
动物(4)
植物(3)
微生物(2)也就是按照t_name这个字段所含有的f_name的数量从大到小排列
语句该如何写?(也可以不取出t_name后面跟的数字)
id t_name f_name1 植物 树
2 植物 花
3 植物 草
4 动物 猪
5 动物 狗
6 动物 牛
7 动物 羊
8 微生物 XXXX菌
9 微生物 AAAA菌其中id是自增加字段,现在想select达到这样的效果:
动物(4)
植物(3)
微生物(2)也就是按照t_name这个字段所含有的f_name的数量从大到小排列
语句该如何写?(也可以不取出t_name后面跟的数字)
insert into @t select 1,'植物','树'
union all select 2,'植物','花'
union all select 3,'植物','草'
union all select 4,'动物','猪'
union all select 5,'动物','狗'
union all select 6,'动物','牛'
union all select 7,'动物','羊'
union all select 8,'微生物','XXXX菌'
union all select 9,'微生物','AAAA菌'
select t_name+'('+cast(count(*) as varchar)+')' from @t group by t_name
insert into @t select 1,'植物','树'
union all select 2,'植物','花'
union all select 3,'植物','草'
union all select 4,'动物','猪'
union all select 5,'动物','狗'
union all select 6,'动物','牛'
union all select 7,'动物','羊'
union all select 8,'微生物','XXXX菌'
union all select 9,'微生物','AAAA菌'
select t_name+'('+cast(count(*) as varchar)+')' from @t group by t_name order by count(*) desc
--排下序