为什么DB :: table中的leftJoin结果与DB :: select中的左联接不同?

时间:2019-04-02 04:05:44

标签: php laravel laravel-5.7 laravel-pagination

我正在使用DB :: select和DB :: table,但结果不同。我不知道为什么DB :: table中的leftJoin与DB :: select中的左联接有不同的结果。 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

1 个答案:

答案 0 :(得分:0)

以上代码中的leftJoin与join子句的结果相同。我使用高级leftJoin子句解决了该问题,如下所示。

$ awk -F'[:"]' 'NR==FNR{a[$0];next} /Type.*ABC/ && !($(NF-1) in a)' excludes file
    Type: "ABC::DEF::AVC"