简体   繁体   中英

DELETE FROM multiple tables?

Anyone have an idea on how to delete from multiple tables in one query? I have been scratching my head endless trying to find a way to make this work. But my endless search on google havnet helped..

I have a delete button on my site that needs to delete from multiple tables.. Right now, it uses the code to delete from the the table "tilbehor"

DELETE FROM tilbehor WHERE t_id = '$id'

But I also need it to delete from the table "kategori"

In the kategori table i have the 3 columns "id", "p_id" and "t_id". I need it all to be deleted where "t_id" = the $id from the query

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

If the t_id is the main ID. You could look into Foreign keys and use on DELETE Cascade. Foreign keys are connected with a primary key from another table. When the matching primary key of the table is deleted, the rows on other tables that match the foreign key with the primary key are deleted as well

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM