【问题标题】:how to compare more than one column from two different tables of database and update, if it match the same record in laravel如果它与laravel中的相同记录匹配,如何比较两个不同数据库表中的多个列并更新
【发布时间】:2021-04-24 21:15:22
【问题描述】:

我想在比较表resignationrequest 中的两列employee_id & job_id 的数据后更新列completed 的数据。在我的 laravel 控制器中需要写什么代码。

*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         |

我在控制器中编写了以下代码,但它不起作用。

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

【问题讨论】:

  • 你的代码看起来不错你有什么具体问题要问
  • 我也觉得我的代码很好,但它没有按照我的意愿更新任何记录。我实际上想为特定条件匹配两个表的employee_id、job_id 列,然后用1 完成更新

标签: php laravel laravel-blade laravel-8


【解决方案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' ]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-11
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    相关资源
    最近更新 更多