简体   繁体   中英

Mysql - Voting once per Day

I have an entry where users can vote once per day. I''m saving this in my database.
Now I need to check, if the user is allowed to vote on this entry again (after one day).

So far I got this:

SELECT count(*) 
FROM entries e
WHERE e.voterID =1
AND e.pID =1
AND e.date < NOW( ) - INTERVAL 1 
DAY   

But this doesn't work to well, when in the DB there are more entries for the voter and pid. A voter can vote multiples times for the same entry.
if there are more entries for the same user and the same projects, count(*) fives me a value more then 1. etc.

How do I check if the user is allowed to vote again properly?
Thanks.

Did the present user vote for the present pID in the most recent 24 hours? If so, the votecount result in this query will be more than zero.

 SELECT count(*) AS votecount
   FROM entries AS e
  WHERE e.voterID = 1
    AND e.pID = 1
    AND e.date >= NOW() - INTERVAL 1 DAY

Notice the >= comparison for the date. Your sample code says < .

You will need a compound index on (voterID, pID, date) when you need this query to run efficiently on a large table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM