Delete bitsCN.com from another table
Delete another table based on the table
DELETE: how can I DELETE data from multiple associated tables at the same time? Here is an in-depth explanation:
1. delete from t1 where condition
2. delete t1 from t1 where condition
3. delete t1 from t1, t2 where conditions
4. delete t1, t2 from t1, t2 where conditions
The first three are feasible, and 4th are not feasible.
That is to say, a simple delete statement cannot be used to delete data from multiple tables. However, you can create a cascading delete relationship between two tables, you can delete the data in another table at the same time.
1. delete all matching records with id values in table T2.
DELETE t1 FROM t1, t2 WHERE t1.id = t2.id or delete from t1 USING t1, t2 WHERE t1.id = t2.id
2. find and delete no matching records in data table T2.
DELETE t1 FROM t1 left join T2 ON t1.id = t2.id WHERE t2.id is null or delete from t1, USING t1 left join T2 ON t1.id = t2.id WHERE t2.id IS NULL
3. find the data with the same record from the two tables and delete the data from both tables.
DELETE t1, t2 from t1 left join t2 ON t1.id = t2.id WHERE t1.id = 25
Note that delete t1, t1 in t2 from, and t2 cannot be aliases.
For example, delete t1, t2 from table_name as t1 left join table2_name as t2 on t1.id = t2.id where table_name.id = 25 the execution in the data is incorrect (MYSQL version no less than 5.0 is acceptable in MYSQL 5.0)
Write the preceding statement
Delete table_name, table2_name from table_name as t1 left join table2_name as t2 on t1.id = t2.id where table_name.id = 25 execution in the data is incorrect (MYSQL versions earlier than 5.0 are acceptable in MYSQL 5.0)
BitsCN.com