2

When i use $query2->union($query1) this is the result sql in laravel:

query one ...
union
query two ...

How can i have this query:

query one ...
union distinct
query two ...

In laravel documentation i saw only union() and unionAll() methods.

Morteza
  • 2,097
  • 5
  • 28
  • 47
  • Please refer to: http://stackoverflow.com/questions/49925/what-is-the-difference-between-union-and-union-all – codeblur Dec 01 '15 at 03:03

1 Answers1

1

As per this answer, a UNION query by definition is a distinct query (assuming the columns in the two results are the same). A UNION ALL returns duplicates if they exist.

So, in Laravel 5.1 (at least), running a union as per the example in the documentation is, by default, distinct:

$first = DB::table('users')
    ->whereNull('first_name');

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

I'm not going to repeat what has been said well in that answer regarding performance (it's worth reading) but in summary a DISTINCT is slower than a DISTINCT ALL.

Community
  • 1
  • 1
dKen
  • 3,078
  • 1
  • 28
  • 37