我如何在MySQL中统计投票?

时间:2010-11-18 20:04:38

标签: sql mysql

我有一个名为votes的数据库表,其中包含三列'timestamp','voter'和'voted_for'。

表中的每个条目代表一票。我想在某些条件下为每个'voted_for'计算所有选票。

条件如下:

  • 每位选民只能投票一次,如果一位选民多次投票,最近的投票就会计算在内。

  • 只计算在指定时间之前进行的投票。

3 个答案:

答案 0 :(得分:4)

试试这个:

SELECT voted_for, count(*)
FROM votes v
INNER JOIN (SELECT Voter, Max(timestamp) as lastTime from votes group by Voter) A 
on A.Voter = v.voter and a.lasttime = v.timestamp 
WHERE timestamp < {date and time of last vote allowed}
Group by voted_for

答案 1 :(得分:0)

SELECT voted_for,COUNT(DISTINCT voter)
FROM votes
WHERE timestamp < '2010-11-18 21:05:00'
GROUP BY voted_for

答案 2 :(得分:0)

以下内容可能会有所帮助:

drop table if exists users;
create table users
( 
user_id int unsigned not null auto_increment primary key,
username varbinary(32) not null,
unique key users_username_idx(username)
)engine=innodb;

insert into users (username) values 
('f00'),('foo'),('bar'),('bAr'),('bish'),('bash'),('bosh');

drop table if exists picture;
create table picture
( 
picture_id int unsigned not null auto_increment primary key,
user_id int unsigned not null, -- owner of the picture, the user who uploaded it
tot_votes int unsigned not null default 0, -- total number of votes 
tot_rating int unsigned not null default 0, -- accumulative ratings 
avg_rating decimal(5,2) not null default 0, -- tot_rating / tot_votes
key picture_user_idx(user_id)
)engine=innodb;

insert into picture (user_id) values 
(1),(2),(3),(4),(5),(6),(7),(1),(1),(2),(3),(6),(7),(7),(5);


drop table if exists picture_vote;
create table picture_vote
( 
picture_id int unsigned not null,
user_id int unsigned not null,-- voter
rating tinyint unsigned not null default 0, -- rating 0 to 5
primary key (picture_id, user_id)
)engine=innodb;

delimiter #

create trigger picture_vote_before_ins_trig before insert on picture_vote
for each row
proc_main:begin

declare total_rating int unsigned default 0;
declare total_votes int unsigned default 0;

 if exists (select 1 from picture_vote where 
    picture_id = new.picture_id and user_id = new.user_id) then
        leave proc_main;
 end if;

 select tot_rating + new.rating, tot_votes + 1 into total_rating, total_votes 
  from picture where picture_id = new.picture_id;

 -- counts/stats
 update picture set 
    tot_votes = total_votes,
    tot_rating = total_rating,
    avg_rating = total_rating / total_votes
 where picture_id = new.picture_id;

end proc_main #

delimiter ;

insert into picture_vote (picture_id, user_id, rating) values 
(1,1,5),(1,2,3),(1,3,3),(1,4,2),(1,5,1), 
(2,1,1),(2,2,2),(2,3,3),(2,4,4),(2,5,5),(2,6,1),(2,7,2),
(3,1,5),(3,2,5),(3,3,5),(3,4,5),(3,5,5),(3,6,5),(3,7,5);

select * from users order by user_id;
select * from picture order by picture_id;
select * from picture_vote order by picture_id, user_id;
相关问题