where 子句中的内部查询 laravel
Inner query in where clause laravel
我有一个数据库,其中有两个 table driver_company_map
和 company
我所做的是从 driver_company_map
table 然后我将它传递到我在 company
table 上执行的另一个查询的 where 子句中我的两个单独的查询都是这样工作的
$result = DB::connection($this->masterDb)->table('driver_company_map')
->where('driver_code', $driverCode) //i get the $driverCode from function parameter
->select('company_code')
->first();
$companyCode = $result->company_code;
我在下面的查询中使用上面的 $companyCode
$result = DB::connection($this->masterDb)->table('company')
->where('code', $companyCode)
->select('db_connection')
->first();
$clientDb = $result->db_connection;
上面的逻辑工作正常,但我希望两者都作为嵌套查询我试过了但没有给出正确的结果下面是我的代码
$result = DB::connection($this->masterDb)->table('company')
->where('code', function($companyCode_query){
$companyCode_query->select('company_code')
->from('driver_company_map')
>where('driver_code', $driverCode);
})->get()
->select('db_connection')
->first();
$clientDb = $result->db_connection;
试试这个:
$result = DB::connection($this->masterDb)->table('company')
->select('db_connection')
->join('driver_company_map', 'company.code', 'driver_company_map.company_code')
->where('driver_company_map.driver_code', $driverCode)
->first();
我有一个数据库,其中有两个 table driver_company_map
和 company
我所做的是从 driver_company_map
table 然后我将它传递到我在 company
table 上执行的另一个查询的 where 子句中我的两个单独的查询都是这样工作的
$result = DB::connection($this->masterDb)->table('driver_company_map')
->where('driver_code', $driverCode) //i get the $driverCode from function parameter
->select('company_code')
->first();
$companyCode = $result->company_code;
我在下面的查询中使用上面的 $companyCode
$result = DB::connection($this->masterDb)->table('company')
->where('code', $companyCode)
->select('db_connection')
->first();
$clientDb = $result->db_connection;
上面的逻辑工作正常,但我希望两者都作为嵌套查询我试过了但没有给出正确的结果下面是我的代码
$result = DB::connection($this->masterDb)->table('company')
->where('code', function($companyCode_query){
$companyCode_query->select('company_code')
->from('driver_company_map')
>where('driver_code', $driverCode);
})->get()
->select('db_connection')
->first();
$clientDb = $result->db_connection;
试试这个:
$result = DB::connection($this->masterDb)->table('company')
->select('db_connection')
->join('driver_company_map', 'company.code', 'driver_company_map.company_code')
->where('driver_company_map.driver_code', $driverCode)
->first();