簡體   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

在比較每個表resignationrequest中的兩個列employee_id 和 job_id的數據,我想更新列的數據。 在我的 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         |

我在我的 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' ]);

我用下面的代碼解決了這個問題。

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