2

我想删除数据库中所有在另一个表中没有引用的行:

还行吧:

mysql> DELETE FROM auth_user WHERE username = 'Sample41';
Query OK, 1 row affected (0.02 sec)

这是不行的:

mysql> DELETE FROM auth_user WHERE username = 'Sample98';
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`mydatabase`.`school_subjects`, CONSTRAINT `user_id_refs_id_9e8f6fd7` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`))

所以我的目标是获得一个查询,该查询将删除在其他表中没有引用的所有用户(他们将以'Sample'开头)(实际上只有一个表'school_subjects')

谢谢你。,

更新:

mysql> describe school_subjects;
+------------------+--------------+------+-----+---------+----------------+
| Field            | Type         | Null | Key | Default | Extra          |
+------------------+--------------+------+-----+---------+----------------+
| id               | int(11)      | NO   | PRI | NULL    | auto_increment |
| name             | varchar(500) | NO   |     | NULL    |                |
| user_id          | int(11)      | NO   | MUL | NULL    |                |
| created_by       | varchar(64)  | NO   |     | NULL    |                |
| created_time     | datetime     | NO   |     | NULL    |                |
| num_of_followers | int(11)      | NO   |     | NULL    |                |
+------------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

mysql> describe auth_user;
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(11)      | NO   | PRI | NULL    | auto_increment |
| username     | varchar(30)  | NO   | UNI | NULL    |                |
| first_name   | varchar(30)  | NO   |     | NULL    |                |
| last_name    | varchar(30)  | NO   |     | NULL    |                |
| email        | varchar(75)  | NO   |     | NULL    |                |
| password     | varchar(128) | NO   |     | NULL    |                |
| is_staff     | tinyint(1)   | NO   |     | NULL    |                |
| is_active    | tinyint(1)   | NO   |     | NULL    |                |
| is_superuser | tinyint(1)   | NO   |     | NULL    |                |
| last_login   | datetime     | NO   |     | NULL    |                |
| date_joined  | datetime     | NO   |     | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+
11 rows in set (0.00 sec)
4

3 回答 3

6

所以我的目标是获得一个查询,该查询将删除在其他表中没有引用的所有用户(他们将以'Sample'开头)(实际上只有一个表'school_subjects')

你有一个自引用表。如果要删除所有记录,则必须包括school_subjectsauth_user。尝试:

DELETE FROM auth_user WHERE username not in (SELECT id FROM auth_user) 
and  username not in (SELECT user_id FROM school_subjects);

更新:如果您没有自引用约束并且只关心“school_subjects”,请尝试以下查询,注意它将删除所有与约束不冲突的记录:

DELETE FROM auth_user WHERE username not in (SELECT user_id FROM school_subjects);

如果您只需要删除一些,您将需要:

DELETE FROM auth_user 
WHERE username in ('SomeRecordToBeDeleted', 'one more') AND
username not in (SELECT user_id FROM school_subjects);
于 2013-01-02T17:23:27.163 回答
1

这将删除“school_subjects”表中没有匹配“user_id”的行。

DELETE FROM auth_user 
WHERE id NOT IN (SELECT user_id FROM school_subjects);
于 2013-01-02T17:18:09.800 回答
1

你可以试试这个——

Delete from auth_user where username like 'Sample%' ANd auth_user.id NOT IN 
( select user_id from school_subjects where auth_user.id = school_subjects.user_id) 
于 2013-01-02T17:22:29.987 回答