【问题标题】:How to merge result from multiple tables in efficient way Laravel如何以有效的方式合并来自多个表的结果 Laravel
【发布时间】:2020-02-15 14:55:45
【问题描述】:

我必须合并 Laravel 中多个表的结果。目前我正在使用以下方式:

public function getMachines(Request $request) {
        $vendor_id = Auth::id();
        $ml_8_machine=ML8Machine::where('vendor_id','=',$vendor_id)->get();
        $ml_16_machine=ML16Machine::where('vendor_id','=',$vendor_id)->get();
        $ml_32_machine=ML32Machine::where('vendor_id','=',$vendor_id)->get();
        $ml_64_machine=ML64Machine::where('vendor_id','=',$vendor_id)->get();
        $ml_96_machine=ML96Machine::where('vendor_id','=',$vendor_id)->get();
        $ml_128_machine=ML128Machine::where('vendor_id','=',$vendor_id)->get();
        $machines = collect($ml_8_machine)
                        ->merge($ml_16_machine)
                        ->merge($ml_32_machine)
                        ->merge($ml_64_machine)
                        ->merge($ml_96_machine)
                        ->merge($ml_128_machine);
        return view('vendor.machines', compact('machines'));      
    }

我正在寻找一种有效的方法来执行查询并将结果合并到一个集合中。

【问题讨论】:

  • 这些机器学习表有相同的列吗?

标签: mysql sql laravel laravel-5 eloquent


【解决方案1】:

如果您的列相同, 您可以使用unionAll 来合并记录。这将降低 IO 成本:

public function getMachines(Request $request) {
        $vendor_id = Auth::id();
        $ml_8_machine=ML8Machine::where('vendor_id','=',$vendor_id)->select('column1', 'column2'...);
        $ml_16_machine=ML16Machine::where('vendor_id','=',$vendor_id)->select('column1', 'column2'...);
        $ml_32_machine=ML32Machine::where('vendor_id','=',$vendor_id)->select('column1', 'column2'...);
        $ml_64_machine=ML64Machine::where('vendor_id','=',$vendor_id)->select('column1', 'column2'...);
        $ml_96_machine=ML96Machine::where('vendor_id','=',$vendor_id)->select('column1', 'column2'...);
        $ml_128_machine=ML128Machine::where('vendor_id','=',$vendor_id)->select('column1', 'column2'...);
        $machines = $ml_8_machine->unionAll($ml_16_machine)
                        ->unionAll($ml_32_machine)
                        ->unionAll($ml_64_machine)
                        ->unionAll($ml_96_machine)
                        ->unionAll($ml_128_machine)
                        ->get();
        return view('vendor.machines', compact('machines'));      
    }

请记住,您需要保持每列与其他列的顺序相同。

您也可以在此查询中添加paginate()

$machines = $ml_8_machine->unionAll($ml_16_machine)
                        ->unionAll($ml_32_machine)
                        ->unionAll($ml_64_machine)
                        ->unionAll($ml_96_machine)
                        ->unionAll($ml_128_machine)
                        ->paginate(10);

【讨论】:

    【解决方案2】:

    也许其中一种方法可以帮助你

    $vendor_id = Auth::id();
    
    $ml_8_machine=ML8Machine::where('vendor_id','=',$vendor_id)->get();
    $ml_16_machine=ML16Machine::where('vendor_id','=',$vendor_id)->get();
    $ml_32_machine=ML32Machine::where('vendor_id','=',$vendor_id)->get();
    $ml_64_machine=ML64Machine::where('vendor_id','=',$vendor_id)->get();
    $ml_96_machine=ML96Machine::where('vendor_id','=',$vendor_id)->get();
    $machines = ML128Machine::where('vendor_id','=',$vendor_id)
            ->union($ml_8_machine)
            ->union($ml_16_machine)
            ->union($ml_32_machine)
            ->union($ml_64_machine)
            ->union($ml_96_machine)
            ->get();
    
    return view('vendor.machines', compact('machines'));   
    

    或使用原始查询methods

    (SELECT * from ml_8_machine where `vendor_id` = '?')
    UNION
    (SELECT * from ml_16_machine where `vendor_id` = '?')
    

    SELECT * from ml_8_machine, ml_16_machine where `ml_8_machine.vendor_id` = '?' AND `ml_16_machine.vendor_id` = '?'
    

    【讨论】:

      【解决方案3】:

      你可以使用collapse()方法

      $collection = collect([$ml_8_machine, $ml_16_machine, $ml_32_machine, $ml_64_machine, $ml_96_machine, $ml_128_machine]);
      
      $machines = $collection->collapse();
      
      //dump($machines);
      
      return view('vendor.machines', compact('machines')); 
      

      【讨论】:

        猜你喜欢
        • 2018-02-26
        • 2021-12-17
        • 1970-01-01
        • 1970-01-01
        • 2022-01-19
        • 2020-06-15
        • 2018-07-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多