从具有公共字段的多个表中删除

时间:2017-03-02 02:40:47

标签: php mysql

嗨,我有以下几张表......

tbl_favorite_properties

1 favorite_properties_id

2 favorite_properties_property_id

3 favorite_properties_user_id

tbl_property

1 property_id

2 property_user_id

现在我想删除基于用户的字段,如下所示

$user_id = $_SESSION['user_id'];

$delete_tbl_favorite_properties = $con->prepare("DELETE FROM tbl_favorite_properties WHERE favorite_properties_user_id='$user_id'");
$delete_tbl_favorite_properties-> execute();

$delete_tbl_property = $con->prepare("DELETE FROM tbl_property WHERE property_user_id='$user_id'");
$delete_tbl_property-> execute();

现在我要删除tbl_favorite_properties(favorite_properties_property_id)中与tbl_property(property_id)中的此用户属性匹配的所有属性

我已经能够使用mysql在innodb级联删除中执行此操作,但需要一个php解决方案。

这里有一个解决方案How do I delete row with primary key using foreign key from other table?

但是列名相同,我的不同......

我是在多个表中构建查询的新手......

1 个答案:

答案 0 :(得分:1)

我认为您想要的行为是移除属于某个用户的dotnet run中的属性,然后删除tbl_property中相应的收藏夹:

tbl_favorite_properties

最初让我失望的是两个表都有一个user_id列。如果您始终计划通过DELETE t1, t2 FROM tbl_property t1 LEFT JOIN tbl_favorite_properties t2 ON t1.property_id = t2.favorite_properties_property_id WHERE t1.property_user_id = '$user_id' 的加入方式输入该表,则可能不需要tbl_favorite_properties中的user_id。