MySQL从不在的数据库中选择

时间:2013-05-17 14:01:58

标签: mysql sql join

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

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

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

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

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`;

3 个答案:

答案 0 :(得分:1)

我认为not in条款中最干净的方式是select

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_byedited_by列上添加索引,可以提高性能。

答案 1 :(得分: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之前仔细阅读。

答案 2 :(得分:0)

在MySQL中,left outer join ... where null的效果往往优于inexists(有适当的索引),因此以下内容值得尝试:

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;