Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query Builder making wrong query when using UNION with Pagination #2968

Closed
orszaczky opened this issue Dec 16, 2013 · 5 comments
Closed

Query Builder making wrong query when using UNION with Pagination #2968

orszaczky opened this issue Dec 16, 2013 · 5 comments

Comments

@orszaczky
Copy link

Hello,
I tried to make the following paginated UNION query with Query Builder:

$search2 = $search."%";

$query1 = DB::table('fl_foods')
        ->select('id', 'desc')
        ->where('desc', 'LIKE', $search2);

$query = DB::table('fl_foods')
        ->select('id', 'desc')
        ->where('desc', '=', $search)
        ->unionAll($query1)
        ->paginate(10);

For some reason it generates the following aggregate query + error message:

SQLSTATE[21000]: Cardinality violation: 1222 The used SELECT statements have a different number of columns (SQL: select count(*) as aggregate from fl_foods where desc = ? union all select id, desc from fl_foods where desc LIKE ?) (Bindings: array ( 0 => 'but', 1 => 'but%', ))

This error only happens when I'm trying to paginate the results, it makes a proper UNION query when I use ->limit(10)->get();

Also the same thing happens with SELECT * in both queries.

Is there a way to get this to work, or should I just write a some raw query with custom pagination?

@taylorotwell
Copy link
Member

It looks like union and paginate aren't supported right now. I would write the query using the query builder and then use Paginator::make to create a custom Paginator instance.

@ravigehlot
Copy link

Hello Taylor,

I ran into an issue where paginator adds a count column causing a cardinality issue where the number of columns differ on an UNION. I wonder if this issue was addressed in 4.1 or even 4.2. I read the release notes. I didn't find anything on that. Like you mentioned, I used the query builder to retrieve the rows, then I sliced it with array_slice() in PHP to build the paginator. It works. The problem is that DB::select() retrieves all rows before it builds the paginator opposed to setting the offset and limit on the query. This causes major delays. There are solutions to this issue. One would be to change the database by combining both tables. That way I wouldn't need to use UNION. It would be bad to change the database at this point because that would brake the app wherever those tables were used (which is in many places). The other solution would be to build 2 queries and then combine then to call paginator on. What do you recommend?

Best,
Ravi.

@chareice
Copy link

chareice commented Jun 16, 2016

@ravigehlot Hi, Did you solved this problem which load all rows from database for pagination?

@Mechazawa
Copy link
Contributor

Can this issue be re-opened? It's still not possible to use the paginator without workarounds.

@staudenmeir
Copy link
Contributor

This has been fixed in Laravel 5.7.13 (#26365).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants