Reputation: 1763
I was given the task of translating an old query into Zend and all was going well until I went to create the union. I cannot post the actual contents of the query due to company regulations but basically what I have is:
$sql1 = $db->select()
->from(array('t' => 'tableOne'), array('t.id'))
->joinLeft(array('tt' => 'tableTwo'), 'tt.fk_tableOne_id = t.id', array())
->where($db->quoteInto('tt.active = ?', 'Y'));
$sql2 = $db->select()
->from(array('t' => 'tableOne'), array('t.id'))
->joinLeft(array('tt' => 'tableTwo'), 'tt.fk_tableOne_id = t.id', array())
->where($db->quoteInto('tt.active = ?', 'Y'));
$select = $db->select()->union(array($sql1, $sql2))->order('t.id');
Now, if I do a fetchAll on $sql1, it works. If I do a fetchAll on $sql2, it works. However, when I do a fetchAll on $select I get an error 1064 Syntax Error.
The sql string echoed by echo $select
is basically
(ORDER BY `t.id` ASC) UNION (SELECT ... ) UNION (SELECT ...)
With the syntax error near ORDER BY ...
It seems like this should all be working since the two queries work independently, any ideas?
Upvotes: 0
Views: 358
Reputation: 12320
I tried a slightly modified query from yours:
$sql1 = $zdb->select()
->from(array('t' => 'articles'), array('t.id'))
->joinLeft(array('tt' => 'users'), 'tt.id = t.author_id', array())
->where($zdb->quoteInto('tt.level = ?', 'editor'));
$sql2 = $zdb->select()
->from(array('t' => 'blogs'), array('t.id'))
->joinLeft(array('tt' => 'users'), 'tt.id = t.updated_by', array())
->where($zdb->quoteInto('tt.level = ?', 'editor'));
$select = $zdb->select()->union(array($sql1, $sql2))->order('id');
echo $select;
and got the following:
SELECT `t`.`id` FROM `articles` AS `t` LEFT JOIN `users` AS `tt` ON tt.id = t.author_id
WHERE (tt.level = 'editor') UNION SELECT `t`.`id` FROM `blogs` AS `t` LEFT JOIN `users`
AS `tt` ON tt.id = t.updated_by WHERE (tt.level = 'editor') ORDER BY `id` ASC
What version of the framework do you have?
Upvotes: 1