我正在寻找一个我在zend apllication中遇到的问题的解决方案,
我正在使用zf 1.12和php 5.3和我的sql,
以下是我的查询,它在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 frame environment query中转换该查询,
这与我的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;
所以,如果有人可以帮我创建和格式化非常有用的查询
答案 0 :(得分:3)
试试这个:
$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;
Doc:http://framework.zend.com/manual/1.12/en/zend.db.select.html