简体   繁体   中英

Zend Framework 1 cannot use where() method when using union()

$select = $logAdapter->select(); // an instance of Zend_Db_Select class
$select->union(array(
         "SELECT Country,Name
          FROM ManualLog_TheNew",
         "SELECT Country,Name
          FROM ManualLog_TheOld"),Zend_Db_Select::SQL_UNION_ALL);
$select->order("$param->orderBy")
         ->limit($param->length,$param->offset);

This work, but when I insert where() method before $select->order() , will throw error.

$select->where('ManualLog_TheOld.OperateTime >= ?' => "2014-06-30");

Error is:

<b>Fatal error</b>:  Uncaught exception 'Zend_Db_Select_Exception' with message 'Invalid use of where clause with UNION' in /xxx/code/ZendFramework/Zend/Db/Select.php:880

I've been frustrated for about two days, please help me. Thank you.

@Claudio Venturini

I tried your answer, still have two questions:

1 The sql printed out are:

Warning: strpos() expects parameter 1 to be string, array given in /code/ZendFramework/Zend/Db/Select.php on line 739
Warning: explode() expects parameter 2 to be string, array given in /code/ZendFramework/Zend/Db/Select.php on line 740
Warning: strpos() expects parameter 1 to be string, array given in /code/ZendFramework/Zend/Db/Select.php on line 739
Warning: explode() expects parameter 2 to be string, array given in /code/ZendFramework/Zend/Db/Select.php on line 740
SELECT `ManualLog_TheOld`.`Country` 
FROM `` AS `ManualLog_TheOld` 
WHERE (OperateTime >= '2014-06-30') 
UNION ALL 
SELECT `ManualLog_TheNew`.`Country` 
FROM `` AS `ManualLog_TheNew` 
WHERE (OperateTime >= '2014-06-30') 
ORDER BY `OperateTime` desc LIMIT 200

How to remove the from ``...as ?

2 When I remove the from `` ... as and execute the sql, I got error below:

Error Code: 1054
Unknown column 'ManualLog_TheNew.OperateTime' in 'order clause'

But I have OperateTime field! What's wrong?

PS: I've got it, I should query the OperateTime field out too.

You cannot use a WHERE condition on the outer UNION query. You can only use WHERE inside any of the subqueries in the UNION .

For example this is valid SQL

(SELECT Country,Name
    FROM ManualLog_TheNew
    WHERE condition1)
UNION
(SELECT Country,Name
    FROM ManualLog_TheOld
    WHERE condition2)

while the following is not valid:

(SELECT Country,Name FROM ManualLog_TheNew)
UNION
(SELECT Country,Name FROM ManualLog_TheOld)
WHERE condition

See the documentation on UNION for reference: http://dev.mysql.com/doc/refman/5.5/en/union.html

So to add the same WHERE condition programmatically in each condition you have to create each SELECT statement separately. Try the following:

$condition = 'OperateTime >= ?';
$conditionValue = '2014-06-30';

$selectOld = $logAdapter->select();
$selectOld->from(array('ManualLog_TheOld'), array('Country', 'Name'))
$selectOld->where($condition, $conditionValue);

$selectNew = $logAdapter->select();
$selectNew->from(array('ManualLog_TheNew'), array('Country', 'Name'))
$selectNew->where($condition, $conditionValue);

$select = $logAdapter->select();
$select->union(array($selectOld, $selectNew), Zend_Db_Select::SQL_UNION_ALL);

$select->order($param->orderBy)->limit($param->length, $param->offset);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM