我想知道如何使用具有不等列数(比如 3 和 4)的表进行联合/联合所有查询。 我知道我可以在简单的 SQL 中使用 NULL AS col 来实现这一点。
但是我在 Laravel 工作,我想知道是否有可能使用 Query/Builder 或任何其他方式来做到这一点。
最佳答案
这对我有用,使用 laravel 5.2 的 sql 查询构建器
$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();
dd($秒);
关于php - Laravel 中列数不等的联合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38306579/