2011-11-18 25 views
5

Trong sql i am using union tôi không biết cách viết nó trong zend db.Cách sử dụng union trong zend db

select m.*, 0 as is_shared from test m where user_id = $userId 
union 
select m.*,1 as is_shared from test m 
join test_shares ms 
where m.test_id = ms.test_id 
and ms.email_address = $email 
and m.url is not null; 

Xin hãy giúp tôi ra ....

tôi đã cố gắng như thế này nhưng không sử dụng

$cols1 = array('test.*,0 as is_shared'); 
$select1 = $db->select() 
    ->from ('test', $cols1) 
    ->where ('user_id = ?', $userId); 

$cols2 = array('test_shares.*', '1 as is_shared'); 
$select2 = $db->select() 
    ->from ('test', $cols2) 
    ->join ('test_shares', array()) 
    ->where ('test.test_id = test_shares.test_id') 
    ->where ('test_shares.email_address = ?', $email) 
    ->where ('test.url is NOT NULL'); 

$select = $db->select() 
      ->union(array($select1, $select2)) 
      ->order('title'); 

Nó đang 'thử'. '0' AS 'is_shared' như thế này nhưng tôi cần như thế này 0 như is_shared. Nó đang dùng 'thử nghiệm'. '1' AS 'is_shared' như thế này nhưng tôi cần như thế này 1 như is_shared.

Trả lời

8

Tôi không biết sql của chúng tôi là làm việc. Nhưng nó có thể làm cho mã sau đây.

$userId = 10; 
$email = '[email protected]'; 

$select1 = $db->select() 
       ->from(array('m' => 'test'), array('*', '0 AS is_shared')) 
       ->where('user_id =?', $userId); 

$select2 = $db->select() 
       ->from(array('m' => 'test'), array('*', '1 AS is_shared')) 
       ->join(array('ms' => 'test_shares'), 'm.test_id = ms.test_id', '') 
       ->where('ms.email_address =?', $email) 
       ->where('m.url IS NULL');     

$select = $this->select() 
    ->union(array($select1, $select2)) 
    ->order('title'); 

echo $select; die; 

     /*SELECT `m`.*, `m`.`0` AS `is_shared` 
     * FROM `test` AS `m` 
     * WHERE (user_id =10) 
     * UNION 
     * SELECT `m`.*, `m`.`1` AS `is_shared` 
     * FROM `test` AS `m` 
     * INNER JOIN `test_shares` AS `ms` 
     * ON m.test_id = ms.test_id 
     * WHERE (ms.email_address ='[email protected]') AND (m.url IS NULL) 
     * ORDER BY `title` ASC*/ 
1

Theo Zend_Db_Select documentation, bạn có thể tạo một truy vấn cho từng thành viên của liên minh (chúng có thể là chuỗi hoặc Zend_Db_Select đối tượng), sau đó gọi phương thức union()Zend_Db_Select.

Cái gì như:

$sql1 = FIRSTPARTOFTHEQUERY; 
$sql2 = SECONDPARTOFTHEQUERY; 
$select = $db->select(); 
$select->union(array($sql1, $sql2)); 

Hy vọng rằng sẽ giúp,

2

Để tham khảo sau này, trong Zend Framework 2.3, việc này được thực hiện kết hợp.

Ví dụ:

use \Zend\Db\Sql\Select; 
    use \Zend\Db\Sql\Sql; 

    $sql = new Sql(/* ADAPTER HERE */); 

    $tag1 = new Select(['a' => 'articles']); 
    $tag1->columns([ 'tag' => 'first_tag']); 
    $tag1->where->in('a.id', $articleIds); 

    $tag2 = new Select(['a' => 'articles']); 
    $tag2->columns([ 'tag' => 'second_tag']); 
    $tag2->where->in('a.id', $articleIds); 
    $tag2->combine($tag1); 

    $tag3 = new Select(['a' => 'articles']); 
    $tag3->columns([ 'tag' => 'third_tag']); 
    $tag3->where->in('a.id', $articleIds); 
    $tag3->combine($tag2); 

    $statement = $sql->prepareStatementForSqlObject($tag3); 
Các vấn đề liên quan