查询是作为SELECT而不是更新?

时间:2012-08-02 16:29:36

标签: mysql mysql-error-1064

我正在尝试使用联系人表和accounts_contacts连接表在MySQL中执行重复的清理查询。我有查询作为SELECT查询,但当我尝试使其成为更新时,我得到一个非常非特定的错误:

#1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以便在'FROM sugarDB.contacts INNER JOIN附近使用正确的语法(SELECT dupIDs.id FROM(第3行的SELECT ct')

以下是查询:

UPDATE ctUpdate  
    SET ctUpdate.deleted = 1 
    FROM sugarDB.contacts AS ctUpdate 
    INNER JOIN (
        SELECT dupIDs.id
        FROM (
            SELECT ctIDs.id
            FROM sugarDB.contacts AS ctIDs 
            INNER JOIN (
                SELECT ctSource.first_name,
                    ctSource.last_name
                FROM sugarDB.contacts AS ctSource
                GROUP BY ctSource.first_name,
                    ctSource.last_name
                HAVING COUNT(*) > 1
                ORDER BY COUNT(*) DESC
            )
            AS ctSource 
            ON ctIDs.first_name = ctSource.first_name 
                AND ctIDs.last_name = ctSource.last_name
        ) 
        AS dupIDs
        LEFT JOIN sugarDB.accounts_contacts AS a2cIDs
        ON dupIDs.id = a2cIDs.contact_id 
        WHERE a2cIDs.id IS NULL
    )
    AS dupIDs
    ON ctUpdate .id = dupIDs.id
;

我已经把它倒了几天了,我找不到错误。非常感谢任何帮助!

1 个答案:

答案 0 :(得分:3)

There is no FROM clause in UPDATE statements in MySql.

相反,您的联接应该是UPDATE子句的一部分:

UPDATE sugarDB.contacts AS ctUpdate 
    INNER JOIN (
        SELECT dupIDs.id
        FROM (
            SELECT ctIDs.id
            FROM sugarDB.contacts AS ctIDs 
            INNER JOIN (
                SELECT ctSource.first_name,
                    ctSource.last_name
                FROM sugarDB.contacts AS ctSource
                GROUP BY ctSource.first_name,
                    ctSource.last_name
                HAVING COUNT(*) > 1
                ORDER BY COUNT(*) DESC
            )
            AS ctSource 
            ON ctIDs.first_name = ctSource.first_name 
                AND ctIDs.last_name = ctSource.last_name
        ) 
        AS dupIDs
        LEFT JOIN sugarDB.accounts_contacts AS a2cIDs
        ON dupIDs.id = a2cIDs.contact_id 
        WHERE a2cIDs.id IS NULL
    )
    AS dupIDs
    ON ctUpdate .id = dupIDs.id
    SET ctUpdate.deleted = 1 
;