表结构:
表1
电影ID 电影名称 更新时间 点击数量
表2
集数ID 电影ID 集数地址
目的:
统计结果:电影名称 更新时间 点击数量 电影集数(count)
sql 语句
create table movie
(
movie_id int
,movie_name varchar(200)
,clicks int
,updatetime datetime
)
go
insert into movie values(1, '天龙八部', 8, getdate());
insert into movie values(2, '神雕侠侣', 14, getdate());
go
create table movie_detail
(
detail_id int
,movie_id int
,volume varchar(100)
)
go
insert into movie_detail values(1, 1, '10');
insert into movie_detail values(2, 1, '11');
insert into movie_detail values(3, 1, '12');
insert into movie_detail values(4, 2, '1');
insert into movie_detail values(5, 2, '2')
go
select a.movie_id,a.movie_name,a.Clicks,a.UpdateTime,count(b.movie_id) from movie_detail b inner join movie a
on(a.movie_id=b.movie_id) group by a.movie_name, a.Clicks, a.UpdateTime,a.movie_id
或者
select a.movie_id,a.movie_name,a.Clicks,a.UpdateTime,count(0) from movie_detail b inner join movie a
on(a.movie_id=b.movie_id) group by a.movie_name, a.Clicks, a.UpdateTime,a.movie_id
关于count(0)的用法,不可言传,只可意会。
example:
select movie_name,count(0) from movie group by movie_name