php - How Can i Convert this multiple sub query in zend query? -
i have following query gives desired output in mysql , want implement in zend query language, has different approach implement query..
select a.name , b.payment , c.totalproj , d.totaltask , t.activetask , h.hour usermaster left outer join ( select a.u_id , sum(a.totaltime * b.rate) payment ( select u_id , project_id , sum(time_to_sec(case when endtime null timediff (starttime,starttime) else timediff (endtime,starttime) end )) / 3600 totaltime logmaster project_id not null group u_id , project_id ) inner join projecttouser b on a.project_id = b.project_id , a.u_id = b.u_id group b.u_id ) b on a.id = b.u_id left outer join ( select u_id , count(*) totalproj projecttouser group u_id ) c on a.id = c.u_id left outer join ( select assigned_to , count(*) totaltask tasktotarget group assigned_to ) d on a.id = d.assigned_to left outer join ( select assigned_to,count(*) activetask tasktotarget is_active = 0 group assigned_to ) t on a.id = t.assigned_to left outer join ( select u_id, sec_to_time(sum(time_to_sec(case when endtime null timediff (starttime,starttime) else timediff (endtime,starttime) end ))) hour logmaster insert_date >= '2013-08-20' , insert_date <='2013-08-31' group u_id ) h on a.id = h.u_id
so if 1 can guide me in how create query in zend helpful, , appreciated
each of subqueries becomes new zend_query can use table , pass in main query.
for example:
$h = new zend_db_select() ->from('logmaster', array('u_id', 'hour' => new zend_db_expr('sec_to_time(sum(time_to_sec(case when endtime null timediff (starttime,starttime) else timediff (endtime,starttime) end ))))') ->where("insert_date >= '2013-08-20'") ->where("insert_date <= '2013-08-31'") ->group('u_id'); $mainquery = new zend_db_select() ->from('a' => 'usermaster', array('name')) ->joinleft($h, 'a.id = h.u_is', array('hour'));
you create each of subqueries own object , can join them main query. last argument of join function columns subquery should added main query.
with zf's fluid interface can keep joining tables/queries until have built entire query.
http://framework.zend.com/manual/1.12/en/zend.db.select.html
Comments
Post a Comment