3

我通过将几列移动到它们自己的表并使用外键来规范化数据库。我正在尝试更新应用程序中的查询以尽可能使用联接。在删除数据的查询中,我不希望查找键值,而只使用连接运行删除。我发现唯一可行的方法是添加USING到查询中-但这破坏了我对LIMIT子句的使用。

DELETE FROM prism_data
USING prism_data
INNER JOIN prism_players p ON p.player_id = prism_data.player_id
INNER JOIN prism_actions a ON a.action_id = prism_data.action_id
WHERE (a.action = 'water-flow')
LIMIT 5000; <--- Was ok before the using/joins were added

我正在尝试分批删除 5,000 个。如何修改查询以使限制仍然有效?

4

1 回答 1

3

不幸的是,根据文档,该LIMIT子句仅对单个表 DELETE有效:

单表语法:

DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]

多表语法:

DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
    tbl_name[.*] [, tbl_name[.*]] ...
    FROM table_references
    [WHERE where_condition]

或者:

DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
    FROM tbl_name[.*] [, tbl_name[.*]] ...
    USING table_references
    [WHERE where_condition]

http://dev.mysql.com/doc/refman/5.0/en/delete.html


另一方面,允许子查询。也许您可以将查询重构为以下形式?

DELETE FROM prism_data
WHERE .... IS IN (SELECT ....)
LIMIT 5000
于 2013-08-01T01:33:44.600 回答