简体   繁体   中英

Why the result of leftJoin in DB::table different left join in DB::select?

I am using DB::select and DB::table but it has a different result. I don't know why leftJoin in DB::table have result different with left join in DB::select. The leftJoin in DB::table has the result same as Join in DB::select. My SQL as below.

$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);

The result of the DB::select is 30 rows. The result of the DB::table is 4 rows. Why the leftJoin clause of the DB::table have different result of the Left Join of the DB::select? I am using Laravel 5.7.

Thank you so much! BienHV

The leftJoin in the above code will have the same result with the join clause. I fixed the problem with the advanced leftJoin clause as below.

$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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM