-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Union + Order = broken #5162
Comments
It looks like changing the order of Zend\Db\Sql\Select::$specifications fixes the issue. I haven't done any major tests....I've just made sure that my other queries still work (some that do use ORDER/LIMIT/OFFSET, some that don't). I also don't have any other DB engines to test against, so I don't know if this would cause problems for those. Original:
New:
|
Hi I had the same problem how can I modify Zend\Db\Sql\Select::$specifications |
Found the file thx. |
I have not tested this against a real database, but my first inclination is to say that if you want to apply any operations to the union itself, then you likely need to make the union parts a subselect. Here is some pseudocode that demonstrates the idea: $one = (new Zend\Db\Sql\Select)->from('one');
$two = (new Zend\Db\Sql\Select)->from('two');
$one->combine($two);
$oneTwo = (new Zend\Db\Sql\Select)->from(['sub' => $one])->order('id');
echo $oneTwo->getSqlString(); produces pseudo-sql something like: SELECT "sub".* FROM (( SELECT "one".* FROM "one" ) UNION ( SELECT "two".* FROM "two" )) AS "sub" ORDER BY "id" ASC Will this fit your requirements? |
Closing as not an issue. |
Fixed CS Merge branch 'combineunionorder' of git://github.com/samsonasik/zf2 into hotfix/5855 * 'combineunionorder' of git://github.com/samsonasik/zf2: fix T_OBJECT_OPERATOR on php 5.3 fix array syntax for php 5.3 undo change on Select.php, and leave the test case Fixes #5162 Conflicts: tests/ZendTest/Db/Sql/SelectTest.php
@ralphschindler: Sorry for taking so long to reply...I only check this stuff when I'm at work, and work has been pretty crazy. I see the issue has been closed (probably due to my lack of response), but I'm surprised that this issue would be ignored... It looks like your suggestion would work (I also haven't tested it), but it seems to me like a hack. If I were writing this query out by hand, there's no need to wrap the whole thing in another SELECT query. Has anyone managed to test out my proposed solution in any other SQL platforms? |
I'm not sure how you would, in code, demonstrate order over the primary query with the combine, and then order over the full combined result set. |
When writing a UNION query using Zend\Db\Sql\Select::combine(), the ORDER BY clause is added to the first query, not the entire query. I don't see a way to put an ORDER BY on the union-ed query. Here is a code sample:
And here is the resulting query (from SQL Profiler):
Notice that the ORDER BY is on the first query, and not the union-ed query. This leads to the following error message from SQL Server:
If the ORDER BY clause is moved outside, to the end of the query, it works correctly.
The text was updated successfully, but these errors were encountered: