统计文章各种分类总数
create table news(id int,bid int, title varchar(10))
insert into news values(1 , 0 , '分类1')
insert into news values(2 , 0 , '分类2')
insert into news values(3 , 0 , '分类3')
insert into news values(4 , 1 , '文章1')
insert into news values(5 , 1 , '文章2')
insert into news values(6 , 3 , '文章3')
insert into news values(7 , 1 , '文章4')
insert into news values(8 , 2 , '文章5')
insert into news values(9 , 3 , '文章6')
go
select m.title 类别,
(select count(1) from news where bid = m.id) 总数
from news m where bid = 0
order by 总数 desc
drop table news
/*
类别 总数
---------- -----------
分类1 3
分类3 2
分类2 1
(所影响的行数为 3 行)
*/
insert into news values(1 , 0 , '分类1')
insert into news values(2 , 0 , '分类2')
insert into news values(3 , 0 , '分类3')
insert into news values(4 , 1 , '文章1')
insert into news values(5 , 1 , '文章2')
insert into news values(6 , 3 , '文章3')
insert into news values(7 , 1 , '文章4')
insert into news values(8 , 2 , '文章5')
insert into news values(9 , 3 , '文章6')
go
select m.title 类别,
(select count(1) from news where bid = m.id) 总数
from news m where bid = 0
order by 总数 desc
drop table news
/*
类别 总数
---------- -----------
分类1 3
分类3 2
分类2 1
(所影响的行数为 3 行)
*/