DELETing w / Using时限制删除记录

时间:2013-08-01 01:18:17

标签: mysql

我正在通过将多个列移动到他们自己的表并使用外键来规范化数据库。我正在尝试更新应用程序中的查询以尽可能使用联接。在删除数据的查询中,我不想查找键值,只是使用连接运行删除。我发现唯一有效的方法是在查询中添加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个。如何修改查询以使限制仍然有效?

1 个答案:

答案 0 :(得分: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