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 - 向 Zend Framework 站点添加新 View

javascript - 我的 html 表单未提交电子邮件

php - 使用 Zend_Db_Table_Select 编写子查询

php - 如何越来越多地显示数据库结果?

php - 如何在服务器上部署和运行一个 zend 项目?

php - 如何使用数据表和数据映射器?它们有必要吗?

php - ZendFramework 2.0.0beta4 服务管理器配置-子键区别

php - 使用\Zend_Db_Table_Abstract::find($id)。 MySQL SET 字段返回字符串而不是(想要的)int

php - 如何处理用于分析的大型数据集以及不同数量的列?

javascript - 在 Jquery 中使用 php 变量