简体   繁体   English

PDO使用连接从多个表中删除记录

[英]PDO deleting records from multiple tables using joins

i want to delete a table row with a photo table. 我想删除带有照片表的表格行。 there is also a table called photo_translate where i store my alt text in different languages. 还有一个名为photo_translate的表格,我用不同的语言存储我的替代文字。 is this possible using joins? 这是否可以使用连接?

$query=$db->prepare("DELETE FROM photo,photo_tranlate INNER JOIN photo_translate on photo.id=photo_translate.rec_id WHERE photo.rec_id=? and photo.page=?" );
    $query->bindvalue(1,$rec_id);
    $query->bindvalue(2,$page_id);
    $query->execute();

You should set a foreign key with "on delete cascade" on the field photo_translate.rec_id 您应该在字段photo_translate.rec_id上设置带有“on delete cascade”的外键

This way when you delte the record in the "photo" table, the corrisponding record in the "photo_translate" table will be deleted automatically. 这样,当您在“照片”表中删除记录时,“photo_translate”表中的相应记录将自动删除。

This is the correct way to handle this situation 这是处理这种情况的正确方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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