2

我知道 mysql 不允许将 LIMIT 与多个表删除查询一起使用。我已经在网上寻找解决方案,但我不确定如何使它们适应我的需要。这是删除所有不在内容中的内容的查询,以保持表格与其他条件。

DELETE f
    FROM   field_data_body f
           INNER JOIN node n
                   ON f.entity_id = n.nid
           LEFT JOIN content_to_keep k
                  ON n.nid = k.nid
    WHERE  n.type = 'article'
           AND k.nid IS NULL

那么我必须添加哪些选项来添加 LIMIT 参数。

编辑:感谢您的帮助,但大多数答案都使用该where in子句。当我使用它时,我收到一条错误消息“这个版本的 MySQL 还不支持 'LIMIT & IN/ALL/ANY/SOME”子查询'

4

4 回答 4

2
DELETE
FROM   field_data_body
WHERE id in (
    SELECT f.id
    FROM   field_data_body f
           INNER JOIN node n
                   ON f.entity_id = n.nid
           LEFT JOIN content_to_keep k
                  ON n.nid = k.nid
    WHERE  n.type = 'article'
           AND k.nid IS NULL)
LIMIT 10; --or whatever you want
于 2013-01-22T17:07:02.937 回答
2

在选择要删除的行的代码部分添加ORDER BYLIMIT,然后将其放在派生表中并连接回要删除的表:

DELETE f_del
FROM field_data_body AS f_del
   JOIN
      ( SELECT f.PK                            --- the Primary Key of the table
        FROM   field_data_body f
               INNER JOIN node n
                       ON f.entity_id = n.nid
               LEFT JOIN content_to_keep k
                      ON n.nid = k.nid
        WHERE  n.type = 'article'
               AND k.nid IS NULL
        ORDER BY some_column
        LIMIT 100
      ) AS tmp
      ON tmp.PK = f_del.PK ; 
于 2013-01-22T17:13:35.830 回答
0

也许能让你朝着正确的方向前进:

.... WHERE IN (SELECT ... FROM field_data_body WHERE nid IS NULL)

于 2013-01-22T17:04:50.823 回答
0

从另一个问题我能够做到以下几点

DELETE f 
FROM   field_data_body f 
WHERE  f.entity_id IN (SELECT * 
                   FROM   (SELECT f.entity_id 
                           FROM   field_data_body f 
                                  INNER JOIN node n 
                                          ON f.entity_id = n.nid 
                                  LEFT JOIN mbrd_delete_nodes_to_keep k 
                                         ON n.nid = k.nid 
                           WHERE  n.type = 'forum' 
                                  AND k.nid IS NULL 
                                  AND f.entity_type = 'node' 
                           LIMIT  1) AS t) 
于 2013-01-22T20:05:48.417 回答