1 回答

TA贡献1804条经验 获得超3个赞
对于雄辩的建设者
在您的情况下,您不需要使用子表,只需使用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 关注
- 150 浏览
添加回答
举报