0

我试图在 MySQL 中执行重复的清理查询,使用一个联系人表和一个 accounts_contacts 连接表。我将查询用作 SELECT 查询,但是当我尝试将其设为 UPDATE 时,我收到了一个非常不具体的错误:

#1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 3 行的“FROM SugarDB.contacts INNER JOIN (SELECT dupIDs.id FROM (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
;

我已经倒了几天了,我找不到错误。任何帮助是极大的赞赏!

4

1 回答 1

3

MySql 中的语句中没有FROM子句。UPDATE

相反,您的联接应该是您的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 
;
于 2012-08-02T16:33:10.040 回答