简体   繁体   English

如果它与 laravel 中的相同记录匹配,如何比较数据库的两个不同表中的多个列并更新

[英]how to compare more than one column from two different tables of database and update, if it match the same record in laravel

I want to update data of column completed after comparing the data of two columns employee_id & job_id from each of the tables resignation and request .在比较每个表resignationrequest中的两个列employee_id 和 job_id的数据,我想更新列的数据。 What code do need to write in my laravel controller.在我的 laravel controller 中需要写什么代码。

*request table:.*
| ID   | employee_id  | job_id | completed |
| ---- | ------------ |--------|-----------|
| 1    | 1            | 1      | 1         |
| 2    | 2            | 4      | 0         |
| 3    | 3            | 7      | 0         |
| 4    | 1            | 6      | 0         |

*resignation table:.*
| ID   | employee_id  | job_id | action    |
| ---- | ------------ |--------|-----------|
| 1    | 1            | 1      | 1         |
| 2    | 1            | 6      | 0         |
| 3    | 2            | 9      | 0         |

I have written the below code in my controller but it is not working.我在我的 controller 中编写了以下代码,但它不起作用。

    DB::table('request as q')
        ->leftJoin('employee as e','q.employee_id','=','e.ID')
        ->leftJoin('resignation as r','e.ID','=','r.employee_id')
        ->where('r.ID',$ID)->where('r.job_id','=','q.job_id')
        ->update([ 'q.completed' => '1' ]);

I solved this with below code.我用下面的代码解决了这个问题。

DB::table('request')
        ->join('resignation', function ($join) {
            $join->on('request.employee_id', '=', 'resignation.employee_id');
            $join->on('request.job_id', '=', 'resignation.job_id');
        })
        ->where('resignation.ID', '=', $ID)
        ->update([ 'request.completed' => '1' ]);

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

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