Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

It's either a Query Builder or MySQL bug. I just ran into it. Union wraps everything in parenthesis, and MySql doesn't like it.

This is what the Builder produces:

( SELECT... ) UNION ( ( SELECT... ) UNION ( SELECT... ) )

This is what is should produce:

( SELECT... ) UNION ( SELECT... ) UNION ( SELECT... )

I fixed it by modifying \Illuminate\Database\Query\Grammers\MysqlGrammar::compileUnion

protected function compileUnion(array $union)
{
	$joiner = $union['all'] ? ' union all ' : ' union ';
	$sql     = $union['query']->toSql();

	return $joiner.(preg_match( '/^\(.*\)$/', $sql ) ? $sql : "($sql)" );
}
Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.