草庐IT

php - 拉维尔 : How to delete rows from multiple table with same id with only 1 query?

coder 2023-10-23 原文

我有这段代码可以一次性从多个表中删除数据:

DB::table('tb_stikes_register_school')->where('register_id', $_POST['id'])->delete();
            DB::table('tb_stikes_register_guardian')->where('register_id', $_POST['id'])->delete();
            DB::table('tb_stikes_register_student')->where('register_id', $_POST['id'])->delete();

我试图将其缩短为仅 1 个查询,guardianschool 表中的 register_id 是 student 表的外键。我一直在尝试使用连接,但只删除了 student 表记录。有什么解决方法吗?

最佳答案

可能是这样的——还没有测试过

DB::table(DB::raw('FROM tb_stikes_register_school, tb_stikes_register_guardian, tb_stikes_register_student'))
->join(ENTER JOIN INFO) // wasn't clear how your tables were related
->where('register_id', $_POST['id'])
->delete();

或者您可以使用完全原始的查询:

 DB::query('SQL statement here');

基本上重新创建与此类似的东西:delete rows from multiple tables

关于php - 拉维尔 : How to delete rows from multiple table with same id with only 1 query?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21747529/

有关php - 拉维尔 : How to delete rows from multiple table with same id with only 1 query?的更多相关文章

随机推荐