0

我有这个 Sql 代码:

( SELECT `column1` FROM `table_1` WHERE `column2` > 2 )
UNION
( SELECT `column1` FROM `table_2` WHERE `column2` < 10 )
ORDER BY `column1` ASC;

Zend Framework 2 如何做到这一点?

4

1 回答 1

0

Zend Framework 2 Zend\Db\Sql 没有提供 Union 方法,但您必须自己编写或使用 JOIN。

$sql = new Sql($this->adapter);
$select = $sql->select();
$select->from($this->table)
       ->join('table_1', 'table_2.column1 = table_1.column1');

$where = new  Where();
$where->greaterThan('column2', 2) ; //youll have to add a and here with a lessThan
$select->where($where);

$statement = $sql->prepareStatementForSqlObject($select);
$result = $statement->execute();

老实说,我没有尝试这个例子,但它至少应该给你一个想法。

于 2013-10-09T14:03:22.427 回答