duanpiao6679 2014-12-04 19:46
浏览 8
已采纳

同时从2个表中删除

I have two tables in DB

food

id  name  ... food_menu

and food_drinks

food_id    drinks_id

food_menu is equal to drinks_id. On the page I have button next to some food and when I click it is delete that food from table food. The problem is can I delete in the same time related drinks_id from food_drinks in same query?

This is what I use now to delete the food.

if ($stmt = $con->prepare("DELETE FROM food WHERE id = ? LIMIT 1"))
{
    $stmt->bind_param("i",$id);
    $stmt->execute();
    $stmt->close();
}

else
{
    echo "ERROR: could not prepare SQL statement.";
}
$con->close();
  • 写回答

1条回答 默认 最新

  • dongshu7162 2014-12-04 19:52
    关注

    You can delete from multiple tables at once like this

    DELETE f, fd
    FROM food f
    LEFT JOIN food_drinks fd on fd.drinks_id = f.food_menu
    WHERE f.id = ?
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?