Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql laravel where子句中的子查询_Mysql_Laravel_Laravel 5_Laravel 5.3 - Fatal编程技术网

Mysql laravel where子句中的子查询

Mysql laravel where子句中的子查询,mysql,laravel,laravel-5,laravel-5.3,Mysql,Laravel,Laravel 5,Laravel 5.3,我想在mysql中的where子句中获得写子查询。是否可以在laravel中的where子句中使用子查询 SELECT p.*, d.name, d2.name, d2.head, u.id, u.aceid, u.firstname FROM project as p left join department as d on p.department_string_id = d.string_id left join department as d2 on d.pare

我想在mysql中的where子句中获得写子查询。是否可以在laravel中的where子句中使用子查询

SELECT
    p.*, d.name, d2.name, d2.head, u.id, u.aceid, u.firstname
FROM project as p
left join department as d
    on p.department_string_id = d.string_id
left join department as d2
    on d.parent_department_string_id = d2.string_id
LEFT join users as u
    on d2.head_aceid = u.aceid
where p.code in
(select ar.project_code from asset_request as ar
 where ar.request_id= '1718AM0010')
在拉雷维尔,我试过

DB::table('project as p')   
                ->leftJoin('department as d','p.department_string_id','=','d.string_id')
                ->leftJoin('department as d2','d.parent_department_string_id','=','d2.string_id')
                ->leftJoin('users as u','d2.head_aceid','=','u.aceid')
                ->where('p.code','=','USA_0057_07')  //instead of code need to use subquery
                ->select('u.id')
                ->first();

我认为您可以重构查询,用另一个join替换
WHERE
子句中的子查询:

SELECT
    p.*, d.name, d2.name, d2.head, u.id, u.aceid, u.firstname
FROM project as p
LEFT JOIN department as d
    ON p.department_string_id = d.string_id
LEFT JOIN department as d2
    ON d.parent_department_string_id = d2.string_id
LEFT JOIN users as u
    ON d2.head_aceid = u.aceid
INNER JOIN asset_request ar
    ON p.code = ar.project_code
WHERE
    ar.request_id = '1718AM0010'
拉威尔代码:

DB::table('project as p')   
            ->leftJoin('department as d','p.department_string_id', '=', 'd.string_id')
            ->leftJoin('department as d2', 'd.parent_department_string_id', '=', 'd2.string_id')
            ->leftJoin('users as u', 'd2.head_aceid', '=', 'u.aceid')
            ->join('asset_request as ar', 'ar.project_code', '=', 'p.code')
            ->where('ar.request_id', '=', '1718AM0010')
            ->select('u.id')
            ->first();
DB::table('project as p')   
            ->leftJoin('department as d','p.department_string_id', '=', 'd.string_id')
            ->leftJoin('department as d2', 'd.parent_department_string_id', '=', 'd2.string_id')
            ->leftJoin('users as u', 'd2.head_aceid', '=', 'u.aceid')
            ->join('asset_request as ar', 'ar.project_code', '=', 'p.code')
            ->where('ar.request_id', '=', '1718AM0010')
            ->select('u.id')
            ->first();