php - 如何在 zend db 中使用 union

标签 php zend-framework zend-db

在 sql 中我使用的是 union 我不知道如何在 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;

请帮帮我....

我试过了,没用

$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');

它正在接受 'test'.'0' AS 'is_shared' 就像这样,但我需要这样的 0 作为 is_shared。 它正在接受 'test'.'1' AS 'is_shared' 就像这样,但我需要这样的 1 作为 is_shared。

最佳答案

我不知道我们的 sql 是否有效。但它可以制作以下代码。

$userId = 10;
$email = 'bbsdf@sdf.sd';

$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 ='bbsdf@sdf.sd') AND (m.url IS NULL) 
         * ORDER BY `title` ASC*/

关于php - 如何在 zend db 中使用 union,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8181783/

相关文章:

php - 从 fusebox ColdFusion 迁移到 PHP

来自 1 个按钮的 javascript 2 url

php - Zend DB - Max Time 不返回最新时间

zend-framework - 当 orwhere 在 () 中时,将 zend db 与 whare 和 orwhere 一起使用

php-redis - 有没有办法在不序列化的情况下将 PHP 对象存储在 Redis 中?

php - 如何将子域路由到帐户页面?域屏蔽如何工作?

php - 使用php表单在数据库中插入多行

php - Zend_Db 使用多个 AND OR 运算符的复杂 WHERE 子句

php - PHP 上的 shell_exec 和 exec 没有返回字符串

php - 在 MongoDB 中求和 "column"