无法在FROM子句中为更新指定目标表'cms_user_login_attempts'

时间:2015-01-09 10:56:47

标签: php mysql

我收到了以上代码片段的上述顺序:

            $qry_WriteToDatabase = "    INSERT INTO cms_user_login_attempts
                                (
                                    cula_user_id,
                                    cula_date_time,
                                    cula_remote_host
                                )
                    VALUES      (
                                    " . $db->SQLString($row->user_id) . ",
                                    Now(),
                                    " . $db->SQLString($str_RemoteHost, true) . "
                                )";
            $db->query($qry_WriteToDatabase);

            $qry_UpdateCount = "UPDATE cms_user_login_attempts 
                                SET cula_attempt_count = (
                                    SELECT COUNT(*)
                                    FROM cms_user_login_attempts
                                    WHERE cula_user_id = ".$db->SQLString($row->user_id)."
                                    AND cula_date_time > DATE_SUB(NOW(), INTERVAL 24 HOUR))
                                    WHERE  cula_user_id = ".$db->SQLString($row->user_id)." 
                                    AND cula_date_time > DATE_SUB(NOW(), INTERVAL 24 HOUR)";
            $db->query($qry_UpdateCount);

我不知道为什么会收到此错误,有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

您可以将查询编写为

$qry_UpdateCount = "UPDATE cms_user_login_attempts a
    CROSS JOIN (SELECT COUNT(*) user_count
    FROM cms_user_login_attempts
    WHERE cula_user_id = ".$db->SQLString($row->user_id)."
    AND cula_date_time > DATE_SUB(NOW(), INTERVAL 24 HOUR)
) b
SET a.cula_attempt_count = b.user_count
WHERE  a.cula_user_id = ".$db->SQLString($row->user_id)." 
AND a.cula_date_time > DATE_SUB(NOW(), INTERVAL 24 HOUR)";