简体   繁体   English

Laravel 5 - 如何在Query Builder或Eloquent的两个表/查询中使用union?

[英]Laravel 5 - How to use union in more than two tables/queries with Query Builder or Eloquent?

I have 10 tables that i want 'union'. 我有10张桌子,我想要“联盟”。 Here my table name with same fields. 这里我的表名具有相同的字段。

sell_2007
sell_2008
sell_2009
...
sell_2015
sell_2016

In the example given by laravel do union in the two tables only ( https://laravel.com/docs/5.3/queries#unions ), how if the table more than two tables/queries? 在laravel中给出的例子中只有两个表中的联合( https://laravel.com/docs/5.3/queries#unions ),如果表中有两个以上的表/查询? In my case there are 10 tables. 在我的情况下,有10个表。 How to do that with Query Builder or Eloquent? 如何使用Query Builder或Eloquent做到这一点?

Thank you for your help. 谢谢您的帮助。

You can add multiple unions like this; 你可以添加这样的多个联合;

$first = DB::table('sell_2007');
$second = DB::table('sell_2008');

$users = DB::table('users')
        ->union($first)
        ->union($second)
        ->get();

You may find that you get better perfomance to union the tables using RAW SQL query. 您可能会发现使用RAW SQL查询将表联合起来可以获得更好的性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM