目前需要使用SQL实现选票统计功能,统计投票总数、赞成票数、反对票数请指导下如何写命令!如何写啊?请给予指示!
首先,每个被参与选举的人应该有个选票结果字段,比如:xp。可以用1表示赞成,2表示反对。
select 投票总数,赞成票数,反对票数 from
(select count(*) 投票总数 from 表名),
(select count(*) 赞成票数 from 表名 where xp = 1),
(select count(*) 反对票数 from 表名 where xp = 2)
select 投票总数,赞成票数,反对票数 from
(select count(*) 投票总数 from vote) as a,
(select count(*) 赞成票数 from vote where xp = 1) as b,
(select count(*) 反对票数 from vote where xp = 2) as c;
必须为派生出来的表都必须有一个自己的别名
select 赞成票数+反对票数 投票总数,赞成票数,反对票数
from(select
count(1) 赞成票数
from TableName with(nolock)
where VoteType='赞成')Agree,
(select
count(1) 反对票数
from TableName with(nolock)
where VoteType='反对')Disagree