[英]Double MySQL query
I'm currently developing an app with a vote feature. 我目前正在开发具有投票功能的应用程序。 I would like people not to sign in and make it simple, so I collect the IP after every vote.
我希望人们不要登录并简化操作,所以我在每次投票后都会收集IP。 With this feature the IP will never vote on the same thing again.
有了此功能,IP将不会再对同一件事进行投票。
But I find it hard to SELECT
a voting, that hasn't been voting by the IP. 但是我发现很难
SELECT
尚未由IP投票的投票。 I've tried this: 我已经试过了:
SELECT * FROM messages
WHERE state = '-1'
AND id != (SELECT message_id FROM log WHERE ip = '$ip')
How do I do this correctly? 如何正确执行此操作?
If I understood you correctly - this query should help you: 如果我正确理解了您-此查询应为您提供帮助:
SELECT messages.* FROM messages
LEFT JOIN log ON log.message_id = messages.id AND log.ip = '$ip'
WHERE state = '-1' AND log.ip IS NULL
SELECT * FROM messages
WHERE state = '-1'
AND id NOT IN (SELECT message_id FROM log WHERE ip = '$ip')
will do what you say, but I am quite positive it will not do what you want: Eg it will allow only one mobile user of a typical cell tower to vote, but will allow him to vote again, after he has lost signal and regathered it at an other location. 会按照您说的做,但我很肯定不会做您想做的事:例如,它将只允许典型蜂窝塔的一个移动用户投票,但在他失去信号并重新聚集后将允许他再次投票它在另一个位置。
$qry = "SELECT * FROM messages " .
"WHERE state = '-1' AND NOT EXISTS (SELECT 1 FROM log WHERE log.message_id = messages.id ".
"AND ip = '" . $ip . "')";
A (NOT) EXISTS is usually faster than a IN expression (NOT)EXISTS通常比IN表达式快
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.