Issues

ZF-7592: Union syntax error

Description

File: Zend/Db/Select.php Line: 1088

// wrong $sql .= $target;

// right $sql .= '('; $sql .= $target; $sql .= ')';


$select = $db->select()->union(array($select1, $select2))->order('field1ASC')->limit(3);
// expected (SELECT table1.id FROM table1  WHERE foo = 'bar') UNION (SELECT table2.id FROM table2 WHERE foo = 'bar') ORDER BY field1 ASC LIMIT 3 
// receive SELECT table1.id FROM table1  WHERE foo = 'bar' UNION SELECT table2.id FROM table2 WHERE foo = 'bar' ORDER BY field1 ASC LIMIT 3 

To fixed it i must do next:


$select->union(array('(' . $select1 . ')', '(' . $select2 . ')'))->order('field1ASC')->limit(3);

it is wrong

Comments

also you may change this 2 line: from $parts = count($this->_parts[self::UNION]); to $parts = count($this->_parts[self::UNION]) - 1; and from $cnt < $parts - 1 to $cnt < $parts

Which database are you using? MySQL or PostgreSQL? I have just checked this syntax (without bracket sign) in Oracle and it works OK.

MySQL

SELECT * FROM test WHERE test_status = 4 ORDER BY r_id DESC UNION SELECT * FROM test WHERE test_status = 3 ORDER BY r_id ASC

#

Error Code : 1221 Incorrect usage of UNION and ORDER BY

#

(SELECT * FROM test WHERE test_status = 4 ORDER BY r_id DESC) UNION (SELECT * FROM test WHERE test_status = 3 ORDER BY r_id ASC)

#

OK

#

This is a duplicate of ZF-4338.

Hello... Is this not a critical bug? No one is assigned to it.

Someone is assigned to it. However, because this issue has already been reported before, this issue was closed (hence the term 'duplicate'). Please also see ZF-4338 .