1 回答

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超3個(gè)贊
對于雄辯的建設(shè)者
在您的情況下,您不需要使用子表,只需使用union 沒有子表:
$a = Customers::select('name', DB::raw("'Customer' AS `type`"));
$b = Suppliers::select('name', DB::raw("'Supplier' AS `type`"));
$a->union($b)->orderBy('type')->limit(20);
如果你想使用subtable,你可以這樣做:
$a = Customers::select('name', DB::raw("'Customer' AS `type`"));
$b = Suppliers::select('name', DB::raw("'Supplier' AS `type`"));
$c = $a->union($b);
Customers::selectRaw('a.name, a.type')
->from(DB::raw("(".$c->toSql().") AS a"))
->mergeBindings($c->getQuery()) // if you have parameters
->orderBy('type')
->limit(20);
對于查詢生成器
你可以這樣做:
$a = Customers::select('name', DB::raw("'Customer' AS `type`"));
$b = Suppliers::select('name', DB::raw("'Supplier' AS `type`"));
$c = $a->union($b);
DB::table(DB::raw("({$c->toSql()}) AS a"))
->mergeBindings($c->getQuery())
->orderBy('a.type')
->select('a.name', 'a.type')
->limit(20);
- 1 回答
- 0 關(guān)注
- 179 瀏覽
添加回答
舉報(bào)