1

如果其他数据库中不存在,我需要根据该数据库从一个数据库中清除记录。
很难解释,所以这里是例子:

Table Users
-----------
id
username
password

Table Articles
--------------
id
title
created_by
edited_by

created_bydeleted_by包含用户 ID。我有 3-4 个表,其结构几乎与文章表相同,我想从表用户中删除在文章表中没有任何记录的用户。我的意思是在and表
中的任何类似文章的表中都找不到 ID 的用户。 怎么做? 我先试了下是否可以根据用户选择所有表的所有数据,但是服务器无法执行查询:created_byedited_by

SELECT * FROM `users` 
JOIN `articles` 
ON `articles`.`created_by` = `users`.`id`  
AND `articles`.`edited_by` = `users`.`id` 
JOIN `articles_two` 
ON `articles_two`.`created_by` = `users`.`id` 
AND `articles_two`.`edited_by` = `users`.`id` 
JOIN `articles_three` 
ON `articles_three`.`created_by` = `users`.`id` 
AND `articles_three`.`edited_by` = `users`.`id` 
JOIN `articles_four` 
ON `articles_four`.`created_by` = `users`.`id` 
AND `articles_four`.`edited_by` = `users`.`id` 
JOIN `articles_five` 
ON `articles_five`.`created_by` = `users`.`id` 
AND `articles_five`.`edited_by` = `users`.`id` 
JOIN `articles_six` 
ON `articles_six`.`created_by` = `users`.`id` 
AND `articles_six`.`edited_by` = `users`.`id`;
4

3 回答 3

1

我认为最干净的方法是not inselect条款中:

select *
from users u
where u.id not in (select created_by from articles where created_by is not null) and
      u.id not in (select edited_by from articles where edited_by is not null) and
      u.id not in (select created_by from articles_two where created_by is not null) and
      u.id not in (select edited_by from articles_two where edited_by is not null) and
      u.id not in (select created_by from articles_three where created_by is not null) and
      u.id not in (select edited_by from articles_three where edited_by is not null) and
      u.id not in (select created_by from articles_four where created_by is not null) and
      u.id not in (select edited_by from articles_four where edited_by is not null)

created_by应该通过在各种和edited_by列上建立索引来帮助提高性能。

于 2013-05-17T14:08:01.797 回答
0

这应该有效。它不是非常优雅,但我认为它很容易理解:

DELETE FROM Users
WHERE ID NOT IN (
  SELECT Created_By FROM Articles
  UNION SELECT Edited_By FROM Articles
  UNION SELECT Created_By FROM Articles_Two
  UNION SELECT Edited_By FROM Articles_Two
  ...
  UNION SELECT Created_By FROM Articles_Six
  UNION SELECT Edited_By FROM Articles_Six
)

与任何大的“清理”查询一样,(a) 首先制作表格的副本,(b) 在键入之前仔细检查COMMIT

于 2013-05-17T14:07:37.363 回答
0

在 MySQL 中,left outer join ... where null往往比inor表现更好exists(在有适当索引的地方),所以以下应该值得尝试:

SELECT u.* FROM `users` u
LEFT JOIN `articles` ac1 ON ac1.`created_by` = u.`id`  
LEFT JOIN `articles_two` ac2 ON ac2.`created_by` = u.`id` 
LEFT JOIN `articles_three` ac3 ON ac3.`created_by` = u.`id` 
LEFT JOIN `articles_four` ac4 ON ac4.`created_by` = u.`id` 
LEFT JOIN `articles_five` ac5 ON ac5.`created_by` = u.`id` 
LEFT JOIN `articles_six` ac6 ON ac6.`created_by` = u.`id` 
LEFT JOIN `articles` ae1 ON ae1.`edited_by` = u.`id` 
LEFT JOIN `articles_two` ae2 ON ae2.`edited_by` = u.`id` 
LEFT JOIN `articles_three` ae3 ON ae3.`edited_by` = u.`id` 
LEFT JOIN `articles_four` ae4 ON ae4.`edited_by` = u.`id` 
LEFT JOIN `articles_five` ae5 ON ae5.`edited_by` = u.`id` 
LEFT JOIN `articles_six` ae6 ON ae6.`edited_by` = u.`id`
WHERE COALESCE(ac1.`created_by`,ac2.`created_by`,ac3.`created_by`,
               ac4.`created_by`,ac5.`created_by`,ac6.`created_by`,
               ae1.`edited_by`, ae2.`edited_by`, ae3.`edited_by`, 
               ae4.`edited_by`, ae5.`edited_by`, ae6.`edited_by`)
IS NULL;
于 2013-05-17T14:19:59.803 回答