简体   繁体   中英

Delete from multiple tables with MyISAM

I already tried lots and lots of combinations but none seem to work =(

I have these two SQL commands:

$sql = "DELETE FROM infogt2000_partner_lead_invoice WHERE infogt2000_partner_lead_invoice_lead_id = ".$id_invoice;
$sql = "DELETE FROM infogt2000_partner_lead  WHERE infogt2000_partner_lead_id = ".$id;

And they have to execute "together". Well, if I delete an information from partner_lead it also has to delete the same info from infogt2000_partner_lead_invoice. The id in infogt2000_partner_lead_id and infogt2000_partner_lead_invoice_lead_id are the same but I cannot write it correctly.

I am currently using MySQL and the type is MyISAM

You should consider converting your database tables to InnoDB so that you can create foreign key constraints to cascade on delete. Otherwise, you must write multiple queries, starting from the lowest item in the chain of hierarchy working your way up to the parent-most table. Eg: delete grandchildren, then children, then parents.

I gess you will need to use inner join:

DELETE infogt2000_partner_lead_invoice, infogt2000_partner_lead
FROM infogt2000_partner_lead_invoice
INNER JOIN infogt2000_partner_lead
WHERE infogt2000_partner_lead_invoice.infogt2000_partner_lead_invoice_lead_id = infogt2000_partner_lead.infogt2000_partner_lead_id
AND infogt2000_partner_lead_invoice_lead_id = $id

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