我被赋予了将旧查询翻译成Zend的任务,一切顺利,直到我创建联盟。由于公司规定,我无法发布查询的实际内容,但基本上我所拥有的是:
$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');
现在,如果我在$ sql1上执行fetchAll,它就可以了。如果我在$ sql2上执行fetchAll,它就可以了。但是,当我在$ select上执行fetchAll时,我收到错误1064语法错误。
echo $select
回显的sql字符串基本上是
(ORDER BY `t.id` ASC) UNION (SELECT ... ) UNION (SELECT ...)
ORDER BY附近的语法错误...
似乎这一切都应该有效,因为两个查询独立工作,任何想法?
答案 0 :(得分:1)
我尝试了一些稍微修改过的查询:
$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;
并得到以下内容:
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
你有什么版本的框架?