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

PDO deleting records from multiple tables using joins

我想删除包含照片 table 的 table 行。还有一个名为 photo_translate 的 table,我在其中存储不同语言的替代文本。这可能使用连接吗?

$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();

您应该在字段 photo_translate.rec_id

上设置 foreign key 和 "on delete cascade"

这样删除"photo"table中的记录时,"photo_translate"table中对应的记录也会自动删除

这是处理这种情况的正确方法