1

我正在使用 DB::select 和 DB::table 但它有不同的结果。我不知道为什么 DB::table 中的 leftJoin 与 DB::select 中的 left Join 的结果不同。DB::table 中的 leftJoin 与 DB::select 中的 Join 的结果相同。我的 SQL 如下。

$items = DB::select('select
    element_designs.id as element_design_id, element_designs.doc_no,
    elements.id as element_id, element_designs.project_id as project_id, 
    tag_mappings.id as tag_mappings_id,
    tag_mappings.created_at as create_time,
    element_designs.element_name, elements.element_seq_id,
    edes.material_name
from element_designs 
    join elements
        on element_designs.id = elements.element_design_id 
        and elements.deleted_at is null
    left join tag_mappings 
        on elements.id = tag_mappings.element_id 
        and tag_mappings.deleted_at is null 

where element_designs.disable_flg = false and element_designs.project_id = 1
    order by element_designs.id');

$items = DB::table('element_designs')
    ->join('elements', 'element_designs.id', '=', 'elements.element_design_id')
    ->where('elements.deleted_at', null)
    ->leftJoin('tag_mappings', 'elements.id' , '=', 'tag_mappings.element_id')
    ->where('tag_mappings.deleted_at', null)
    ->where('element_designs.disable_flg', false)
    ->where('element_designs.project_id', 1)
    ->orderBy('element_designs.id', 'asc')
    ->select('element_designs.id as element_design_id', 'element_designs.doc_no', 'elements.id as element_id', 'element_designs.project_id as project_id',
        'tag_mappings.created_at as create_time', 'tag_mappings.id as tag_mappings_id', 'element_designs.element_name', 'elements.element_seq_id',
        'element_designs.material_name')
    ->paginate(20);

DB::select 的结果是 30 行。DB::table 的结果是 4 行。为什么 DB::table 的 leftJoin 子句与 DB::select 的 Left Join 的结果不同?我正在使用 Laravel 5.7。

太感谢了!BienHV

4

1 回答 1

0

上面代码中的 leftJoin 将与 join 子句具有相同的结果。我用下面的高级 leftJoin 子句解决了这个问题。

$items = DB::select('select
    element_designs.id as element_design_id, element_designs.doc_no,
    elements.id as element_id, element_designs.project_id as project_id, 
    tag_mappings.id as tag_mappings_id,
    tag_mappings.created_at as create_time,
    element_designs.element_name, elements.element_seq_id,
    edes.material_name
from element_designs 
    join elements
        on element_designs.id = elements.element_design_id 
        and elements.deleted_at is null
    left join tag_mappings 
        on elements.id = tag_mappings.element_id 
        and tag_mappings.deleted_at is null 

where element_designs.disable_flg = false and element_designs.project_id = 1
    order by element_designs.id');

$items = DB::table('element_designs')
    ->join('elements', function($join){
        $join->on('element_designs.id', '=', 'elements.element_design_id')
            ->where('elements.deleted_at', null);
    })
    ->leftJoin('tag_mappings', function($join){
        $join->on('elements.id' , '=', 'tag_mappings.element_id')
            ->where('tag_mappings.deleted_at', null);
    })
    ->where('element_designs.disable_flg', false)
    ->where('element_designs.project_id', 1)
    ->orderBy('element_designs.id', 'asc')
    ->select('element_designs.id as element_design_id', 'element_designs.doc_no', 'elements.id as element_id', 'element_designs.project_id as project_id',
        'tag_mappings.created_at as create_time', 'tag_mappings.id as tag_mappings_id', 'element_designs.element_name', 'elements.element_seq_id',
        'element_designs.material_name')
    ->paginate(20);
于 2019-04-17T03:57:46.537 回答