在一张表中(表名:TopIc):
字段有
ID: int,主键,自增
HuatiID int 话题ID
BankuaiID int 板块ID
ZhutiTitle varchar(50) 主题名称
Hites int 点击数
表里面插入数据: 如图
使用sql语句:
select * from TopIc
---select distinct BankuaiID from TopIc
select SUM(Hites) as 总点击数,ZhutiTitle as 专题名称,
count(BankuaiID) as 记录条数,
cast((SUM(Hites)*1.0/count(BankuaiID)) as numeric(18,2)) as 平均点击数
from TopIc
where exists(select distinct BankuaiID from TopIc)
group by ZhutiTitle
查询后的显示结果为:
这里为什么用exists(见上面sql语句红色表示处)而不用in,是因为在表里面大数据量时,exists比in效率要高。