2011-07-13 55 views
0

如何在Zend Framework中以這種方式使用UNION ALLPHP Zend Framework - 我如何使用union all?

(select id from astrology where commu_time_from <= '11:51' and commu_time_to >= '11:51' and user_id=1 and appo_date='03/01/2017') union all 
(select id from facereading where commu_time_from <= '11:51' and commu_time_to >= '11:51' and user_id=1 and appo_date='03/01/2017') union all 
(select id from numerology where commu_time_from <= '11:51' and commu_time_to >= '11:51' and user_id=1 and appo_date='03/01/2017') union all 
(select id from palmistry where commu_time_from <= '11:51' and commu_time_to >= '11:51' and user_id=1 and appo_date='03/01/2017') union all 
(select id from solutions where commu_time_from <= '11:51' and commu_time_to >= '11:51' and user_id=1 and appo_date='03/01/2017') union all 
(select id from vastu where commu_time_from <= '11:51' and commu_time_to >= '11:51' and user_id=1 and appo_date='03/01/2017') 

我還需要弄清楚如何跟蹤結果中的總行數。

回答

4

API Docs

union(array $select = array, $type = self) : Zend_Db_Select

添加一個UNION子句的查詢。

第一個參數必須是Zend_Db_Select或sql查詢字符串的數組。

$sql1 = $db->select(); 
$sql2 = "SELECT ..."; 
$select = $db->select() 
      ->union(array($sql1, $sql2)) 
      ->order("id"); 

同樣的例子和一些額外的文本可以在ZF參考指南中的Example #28 Example of union() method找到。除此之外,當您不需要查詢生成器時,您始終可以使用Zend_Db_Expr

2
$select = union($select1, $select2, self::SQL_UNION_ALL). 

例:

$select1 = 'select A from table1'; 
$select2 = 'select A from table2'; 

$select = $db->select()->union($select1, $select2, Zend_Db_Select::SQL_UNION_ALL); 

echo $select; 

結果:(SELECT甲FROM TABLE1)UNION ALL(SELECT甲FROM TABLE2)。

5

您需要將Zend_Db_Select的數組傳遞給union()方法。第二個參數要傳遞給union()方法來執行UNION的類型。在你的情況下,使用Zend_Db_Select::SQL_UNION_ALL作爲常數。

例如,

$sql1 = $db->select(); 
$sql2 = "SELECT ..."; 

$select = $db->select() 
    ->union(array($sql1, $sql2), Zend_Db_Select::SQL_UNION_ALL); 

參考: http://framework.zend.com/manual/1.12/en/zend.db.select.html#zend.db.select.building.union

+1

這是正確的答案。應該被接受 – Darius

0

這裏是在原始的SQL

選擇CONCAT_WS(」」,u.fname,u.lname)作爲運行示例

用戶名,u.image,c。*來自challenge c內部連接challenge_user cu(cu.challenge_id = c.id)內部連接用戶u on(u.id = c.user_id)其中c。 user_id ='1'group by c.id union all 選擇CONCAT_WS('',u.fname,u.lname)作爲user_name,u.image,c。*來自challenge c內部連接challenge_user cu on(cu.challenge_id = c.id)內聯用戶u on(u.id = c.user_id)其中concat('%,',c.challenger_user_id,',%')LIKE concat('%,「1」,%')組通過c.id

在Zend中

$ SQL1 = $這 - >的select() - > setIntegrityCheck(FALSE) - >從(陣列( 'C'=> '挑戰'),陣列('user_name'=> new Zend_Db_Expr(「CONCAT_WS('',u.fname,u.lname)」),'u.image')) - > joinInner(array('cu'=>'challenge_user'), 'cu.challenge_id = c.id')('c.user_id =?','1') - > group('''') C。ID'); (array)('c'=>'challenge'),array('user_name'=> new Zend_Db_Expr(「CONCAT_WS('',u) (array('cu'=>'challenge_user'),'cu.challenge_id = c.id') - > joinInner(array('.fname,u.lname)「),'u.image')) - > joinInner ('u'=>'users'),'u.id = c.user_id') - > where(new Zend_Db_Expr(「concat('%,',c.challenger_user_id,',%')LIKE concat(' %,「1」,%')「)) - > group('c.id'); (數組($($ sql1,$ sql2),Zend_Db_Select :: SQL_UNION_ALL);

$ data = $ this-> fetchAll($ select);

希望這將有助於有人