SELECT COUNT +(从TABLE1 WHERE中选择COUNT)来自Zend中的TABLE1

时间:2012-08-16 06:32:48

标签: mysql zend-framework

如何在Zend中编写此查询

SELECT COUNT( 1 ) AS  `NoOfQueries` , DATE( CONVERT_TZ( FROM_UNIXTIME( poocher.cdate,  '%Y-%m-%d %T' ) ,  '-9:30',  '+00:00' ) ) AS  `Dat` , COUNT( 1 ) + ( 
    SELECT COUNT( 1 ) 
    FROM tblsmsuser poocherb4
    WHERE DATE( CONVERT_TZ( FROM_UNIXTIME( poocherb4.cdate,  '%Y-%m-%d %T' ) ,  '-9:30',  '+00:00' ) ) <= DATE_SUB(  `Dat` , INTERVAL 1 
    DAY ) ) AS cumulative_sum
FROM tblsmsuser poocher
GROUP BY  `Dat`

我试过的是

$this->select = new Zend_Db_Select($this->db);
$selctSub = $this->select->from("tblsmsuser",array("count(1)"))->where("DATE( CONVERT_TZ( FROM_UNIXTIME( tblsmsuser.cdate,  '%Y-%m-%d %T' ) ,  '-9:30',  '+00:00' ) ) <= DATE_SUB(  Dat , INTERVAL 1 DAY )");
echo $select_query_ans = $this->select->from(array("poocher" =>"tblsmsuser"),array("NoOfQueries"=>"count(1)","Dat"=>"DATE( CONVERT_TZ( FROM_UNIXTIME( poocher.cdate,'%Y-%m-%d %T' ),'-9:30','+00:00' ) )","cumulative_sum"=>"count(1) + (".$selctSub.")"))->group("Dat");

输出

SELECT count(1) AS `NoOfQueries`, DATE( CONVERT_TZ( FROM_UNIXTIME( poocher.cdate,'%Y-%m-%d %T' ),'-9:30','+00:00' ) ) AS `Dat`, count(1) + (SELECT count(1) AS `NoOfQueriesb4` FROM `tblsmsuser` AS ```poocherb4`` WHERE (DATE( CONVERT_TZ( FROM_UNIXTIME( poocherb4.cdate, '%Y-%m-%d %T' ) , '-9:30', '+00:00' ) ) <= DATE_SUB( Dat , INTERVAL 1 DAY )))`, count(1) AS `NoOfQueriesb4` FROM `tblsmsuser` AS `poocher` INNER JOIN `tblsmsuser` AS `` WHERE (DATE( CONVERT_TZ( FROM_UNIXTIME( poocherb4.cdate, '%Y-%m-%d %T' ) , '-9:30', '+00:00' ) ) <= DATE_SUB( Dat , INTERVAL 1 DAY )) GROUP BY `Dat` 

1 个答案:

答案 0 :(得分:0)

这是适合我的代码

    $this->select = new Zend_Db_Select($this->db);
    $selctSub = $this->select->from("tblsmsuser",array("count(1)"))->where("DATE( CONVERT_TZ( FROM_UNIXTIME( tblsmsuser.cdate,  '%Y-%m-%d %T' ) ,  '-9:30',  '+00:00' ) ) <= DATE_SUB(  Dat , INTERVAL 1 DAY )");
    $this->select = new Zend_Db_Select($this->db);  //ADDED line
    echo $select_query_ans = $this->select->from(array("poocher" =>"tblsmsuser"),array("NoOfQueries"=>"count(1)","Dat"=>"DATE( CONVERT_TZ( FROM_UNIXTIME( poocher.cdate,'%Y-%m-%d %T' ),'-9:30','+00:00' ) )","cumulative_sum"=>"count(1) + (".$selctSub.")"))->group("Dat");