我已经收到一个MySQL查询,用于删除我要添加LIMIT的数以百万计的整数,这也是ORDERS BY ASC。我不知道我在做什么,我很困。
据我了解,您不能限制多表删除,但是我发现解决这个问题的解决方案太复杂了,我无法自行适应。
DELETE a,b,c
FROM wp_posts a
LEFT JOIN wp_term_relationships b ON ( a.ID = b.object_id )
LEFT JOIN wp_postmeta c ON ( a.ID = c.post_id )
LEFT JOIN wp_term_taxonomy d ON ( d.term_taxonomy_id = b.term_taxonomy_id )
LEFT JOIN wp_terms e ON ( e.term_id = d.term_id )
WHERE d.taxonomy = 'edd_log_type' AND a.post_type = 'edd_log' AND e.term_id = 167
ORDER BY ID ASC LIMIT 100;
期望:我正在尝试构建一个查询,该查询将查找和删除最旧的100个条目。我要设置一个LIMIT,因为要处理的条目(百万个)的数量会使查询超时。
实际结果:就我所知,由于LIMIT导致的语法错误不适用于多表删除。
答案 0 :(得分:1)
您不应在where子句中使用与左联接表相关的列,因为将其用作内部联接 在这些情况下,您应该在ON子句
中移动相关条件要进行删除(根据您的代码),您可以将delete statemente与查询一起返回所需的100 ID
delete a,b,c
FROM wp_posts a
LEFT JOIN wp_term_relationships b ON ( a.ID = b.object_id )
LEFT JOIN wp_postmeta c ON ( a.ID = c.post_id )
LEFT JOIN wp_term_taxonomy d ON ( d.term_taxonomy_id = b.term_taxonomy_id ) AND d.taxonomy = 'edd_log_type'
LEFT JOIN wp_terms e ON ( e.term_id = d.term_id ) AND e.term_id = 167
INNER JOIN (
select a.id
FROM wp_posts a
LEFT JOIN wp_term_relationships b ON ( a.ID = b.object_id )
LEFT JOIN wp_postmeta c ON ( a.ID = c.post_id )
LEFT JOIN wp_term_taxonomy d ON ( d.term_taxonomy_id = b.term_taxonomy_id ) AND d.taxonomy = 'edd_log_type'
LEFT JOIN wp_terms e ON ( e.term_id = d.term_id ) AND e.term_id = 167
WHERE a.post_type = 'edd_log'
ORDER BY ID ASC
LIMIT 100
) t ON t.id = a.id
WHERE AND a.post_type = 'edd_log'
并且正如Strawberry所建议的,使用内部联接更好地进行删除(您不能删除不匹配的行)
delete a,b,c
FROM wp_posts a
INNER JOIN wp_term_relationships b ON ( a.ID = b.object_id )
INNER JOIN wp_postmeta c ON ( a.ID = c.post_id )
INNER JOIN wp_term_taxonomy d ON ( d.term_taxonomy_id = b.term_taxonomy_id ) AND d.taxonomy = 'edd_log_type'
INNER JOIN wp_terms e ON ( e.term_id = d.term_id ) AND e.term_id = 167
INNER JOIN (
select a.id
FROM wp_posts a
INNER JOIN wp_term_relationships b ON ( a.ID = b.object_id )
INNER JOIN wp_postmeta c ON ( a.ID = c.post_id )
INNER JOIN wp_term_taxonomy d ON ( d.term_taxonomy_id = b.term_taxonomy_id ) AND d.taxonomy = 'edd_log_type'
INNER JOIN wp_terms e ON ( e.term_id = d.term_id ) AND e.term_id = 167
WHERE a.post_type = 'edd_log'
ORDER BY ID ASC
LIMIT 100
) t ON t.id = a.id
WHERE AND a.post_type = 'edd_log'