2016-07-11 65 views
2

我想知道如何使用列數不等的表進行聯合/聯合所有查詢(比如說3和4)。 我知道我可以在簡單的SQL中使用NULL作爲色譜柱Laravel中列數不等的聯盟

但是我在Laravel工作,我想知道是否有可能的方式使用Query/Builder或其他任何方式。

+0

的SQL查詢構建器嘗試使用流利的選擇'NULL col'。它可能會工作(或者它可能需要'DB :: raw(「NULL col」)') – apokryfos

回答

1

這個工作對我來說,與laravel 5.2

 $first= DB::table('user_prod') 
      ->select('user_id', DB::raw("NULL as debit")) //shows 'null' because the 'debit' column does not exist in this table 
      ->where('user_id', '=', Auth::user()->id); 

     $second = DB::table('user_transaction') 
      ->select('user_id', DB::raw("debit")) //shows the value of the column 'debit' table 'user_transaction' 
      ->where('user_id', '=', Auth::user()->id); 

      ->union($first) 
      ->get();