尝试检查时出错

时间:2013-04-03 05:19:21

标签: php mysql sql sql-update

我想检查一些事情......但我无法......(查看下面的内容)。

Okey所以这就是它的工作方式,但我想添加更多的支票。

mysql_query('UPDATE characters SET voted=1 where account_name like \''.$row['login'].'\' and online=1;') ;

无论如何我要添加的是在线= 1后检查 哪里 MIN(lastAccess)

我尝试了一些东西,但我失败了...... 像:

mysql_query('UPDATE characters SET voted=1 where account_name like \''.$row['login'].'\' and online=1 having min(lastaccess);') ;

1 个答案:

答案 0 :(得分:0)

您甚至可以根据需要进一步加入表格,

UPDATE  characters a
        INNER JOIN
        (
            SELECT  account_name, MIN(lastaccess) min_date
            FROM    characters
            GROUP   BY account_name
        ) b ON  a.Account_Name = b.Account_name AND
                a.lastAccess = b.min_date
SET     a.voted = 1
WHERE   a.Account_Name = 'nameHere' AND
        a.online = 1

更新1

尝试使用double quotes,例如

$userName = $row['login'];
$result = mysql_query(" UPDATE  characters a
                                INNER JOIN
                                (
                                    SELECT  account_name, MIN(lastaccess) min_date
                                    FROM    characters
                                    GROUP   BY account_name
                                ) b ON  a.Account_Name = b.Account_name AND
                                        a.lastAccess = b.min_date
                        SET     a.voted = 1
                        WHERE   a.Account_Name = '$userName' AND
                                a.online = 1");

作为旁注,如果变量的值( s )来自外部,则查询易受SQL Injection攻击。请查看下面的文章,了解如何防止它。通过使用PreparedStatements,您可以摆脱在值周围使用单引号。