开发者

PHP/MySQL: In a Voting System, how can I keep a user from seeing a profile he has already voted on?

Generally speaking, how can I k开发者_StackOverfloweep a user from seeing a profile he has already voted on so that he does not vote on it twice?

I have a table (called "Users") of users each with a unique user ID (with column "userID"), I have another table (called "Votes") for each vote given by each user to another user (using their respective user ID's with columns "fromUser", "toUser", "vote"), so if I want to show a user a new random profile to vote on, then what is the BEST way to exclude any users where the current User has already voted on (in other words, exclude any "userID" where the "userID" is the "toUser" and where the "fromUser" is the current user's userID).

Thanks!


SELECT userID FROM users
WHERE userID NOT IN (SELECT toUser FROM votes WHERE fromUser = 'theUserIdThatIsVotingNow')
ORDER BY RAND() LIMIT 1


This should do the trick.

SELECT * 
FROM Users 
WHERE userID NOT IN (SELECT toUser FROM Votes WHERE fromUser = '$current_user_userID');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜