且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在不使用外键的情况下删除mysql中多个表中的记录

更新时间:2023-01-29 16:18:19

您好,





您可以在DELETE语句中指定多个表,以根据WHERE子句中的特定条件从一个或多个表中删除行。但是,您不能在多表DELETE中使用ORDER BY或LIMIT。 table_references子句列出了连接中涉及的表。



对于第一个多表语法,只删除FROM子句之前列出的表中的匹配行。对于第二个多表语法,仅删除FROM子句(USING子句之前)中列出的表中的匹配行。结果是你可以同时从许多表中删除行,并有另外的表只用于搜索:



Hi,


You can specify multiple tables in a DELETE statement to delete rows from one or more tables depending on the particular condition in the WHERE clause. However, you cannot use ORDER BY or LIMIT in a multiple-table DELETE. The table_references clause lists the tables involved in the join.

For the first multiple-table syntax, only matching rows from the tables listed before the FROM clause are deleted. For the second multiple-table syntax, only matching rows from the tables listed in the FROM clause (before the USING clause) are deleted. The effect is that you can delete rows from many tables at the same time and have additional tables that are used only for searching:

DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;





或:





Or:

DELETE FROM t1, t2 USING t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;





这些语句在搜索要删除的行时使用所有三个表,但删除仅从表t1和t2匹配行。



前面的示例使用INNER JOIN,但多表DELETE语句可以使用SELECT语句中允许的其他类型的连接,例如作为LEFT JOIN。例如,要删除t1中存在的t2中没有匹配项的行,请使用LEFT JOIN:





These statements use all three tables when searching for rows to delete, but delete matching rows only from tables t1 and t2.

The preceding examples use INNER JOIN, but multiple-table DELETE statements can use other types of join permitted in SELECT statements, such as LEFT JOIN. For example, to delete rows that exist in t1 that have no match in t2, use a LEFT JOIN:

DELETE t1 FROM t1 LEFT JOIN t2 ON t1.id=t2.id WHERE t2.id IS NULL;



语法允许。*在每个tbl_name之后与Access兼容。



详情见以下链接..



http://dev.mysql.com/doc/refman/5.0/en/delete.html [ ^ ]



I希望它能帮到你


The syntax permits .* after each tbl_name for compatibility with Access.

for details see the below link..

http://dev.mysql.com/doc/refman/5.0/en/delete.html[^]

I hope it will help you