php - 使用 zend 框架进行多个子查询

标签 php mysql zend-framework

我正在寻找我在 zend 应用程序中遇到的问题的解决方案,

我在我的 sql 中使用 zf 1.12 和 php 5.3,

这是我的查询,它在 My SQL 中完美运行

SELECT usermaster.*, (select count(projecttouser.u_id) from projecttouser where 
usermaster.id=projecttouser.u_id  ) as proj,

(select count(tasktotarget.assigned_to) from tasktotarget where    
usermaster.id=tasktotarget.assigned_to  ) as target,

(select count(tasktotarget.assigned_to) from tasktotarget where 
usermaster.id=tasktotarget.assigned_to AND tasktotarget.is_active = 1  ) as active


from usermaster  group by usermaster.id

这给出了我想要的完美输出,但在我的sql中

现在我的问题是我必须在 zend 框架环境查询中转换该查询,

这与 my sql 格式有些不同,

我尝试过以下方法,

$psub=$this->select()
            ->setIntegrityCheck(false)
            ->from(array('p'=>'projecttouser'),array('count(p.u_id) as count')) 
            ->join(array('i'=>'usermaster'),'p.u_id=i.id') 
            ->where('i.id=p.u_id');

        $tsub=$this->select()
            ->setIntegrityCheck(false)
            ->from(array('t'=>'tasktotarget'),array('count(t.assigned_to) as tcount'))                     
            ->where('usermaster.id=tasktotarget.assigned_to');

        $tasub=$this->select()
            ->setIntegrityCheck(false)
            ->from(array('ta'=>'tasktotarget'),array('count(ta.assigned_to) as tacount'))                     
            ->where('usermaster.id=tasktotarget.assigned_to AND tasktotarget.is_active = 1 ');

        $sql=$this->select()
                ->setIntegrityCheck(false)                   
                ->from(array('u'=>'usermaster',$psub,$tsub,$tasub))
                 ->group('u.id')   
                 ->order($order_by . ' ' . $order)
                 ->where('u.is_delete=false');                     

        $resultSet = $this->fetchAll($sql);
        return $resultSet;

所以,如果有人可以帮助我创建和格式化查询,那将非常有帮助

最佳答案

试试这个:

$psub=$this->db->select()
    ->setIntegrityCheck(false)
    ->from(array('p'=>'projecttouser'),array('count(p.u_id) as count'))
    //->join(array('i'=>'usermaster'),'p.u_id=i.id') // no need for this join
    ->where('usermaster.id=projecttouser.u_id');

$tsub=$this->db->select()
    ->setIntegrityCheck(false)
    ->from(array('t'=>'tasktotarget'),array('count(t.assigned_to) as tcount'))
    ->where('usermaster.id=tasktotarget.assigned_to');

$tasub=$this->db->select()
    ->setIntegrityCheck(false)
    ->from(array('ta'=>'tasktotarget'),array('count(ta.assigned_to) as tacount'))
    ->where('usermaster.id=tasktotarget.assigned_to AND tasktotarget.is_active = 1 ');

$sql=$this->db->select()
    ->setIntegrityCheck(false)
    ->from(array('u'=>'usermaster'), array('usermaster.*', 
            'proj' => new Zend_Db_Expr('(' . $psub . ')'),
            'target' => new Zend_Db_Expr('(' . $tsub . ')'),
            'active' => new Zend_Db_Expr('(' . $tasub . ')')))
    ->group('u.id')
    //->order($order_by . ' ' . $order)
    ->where('u.is_delete=false');

$resultSet = $this->fetchAll($sql);
return $resultSet;

文档:http://framework.zend.com/manual/1.12/en/zend.db.select.html

关于php - 使用 zend 框架进行多个子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18250697/

相关文章:

PHP mysql_real_escape_string();使用 mysqli 的正确方法是什么?

php - 哪个是最好的 php 框架?

javascript - 如何在 javascript 中捕获 php session 变量而不刷新页面?

表中的 PHP 数组使用 implode()

php - Facebook 登录 : Which parameter should my app identify the user with? id,或电子邮件?

php - 如何在 PHP 中将 13 位字符串转换为 d-m-Y 格式的日期

javascript - 单击浏览器后退按钮时隐藏模态窗口(模态框)

mysql - 连接两个表时可以使用 OR 吗?

mysql - 'PDOException' 消息 'SQLSTATE[HY000] [2002] zend 框架

PHP Zend Lucene 在 "accent-insensitive"中搜索 "case-insensitive"