Zend Framework 2 Union Select with order


Zend Framework 2 Union Select with order

我有这个 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 做到这一点?

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();

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