如何格式化Zend FrameWork的Mysql查询?

时间:2013-05-16 06:27:06

标签: mysql sql zend-framework

我正在zend框架中实现时间跟踪系统,

因为我必须实现以下zend查询:

SELECT
  u_id,
  SEC_TO_TIME(SUM(CASE WHEN TotalTime > 32400
                       THEN TotalTime - 28800
                       ELSE 0 END)) as OverTime,
  SEC_TO_TIME(SUM(TotalTime)) AS TotalTime
FROM 
(
  SELECT
    u_id, insert_date,
    SUM(TIME_TO_SEC(CASE WHEN endtime IS NULL
                         THEN timediff( starttime, starttime )
                         ELSE timediff( endtime, starttime )
                         END ) )  AS TotalTime
  FROM
    logmaster
  WHERE
    project_id IS NOT NULL
    AND logmaster.u_id =1028 
    AND insert_date >('2013-04-01') AND insert_date <('2013-04-31')
  GROUP BY U_ID,insert_date
) AS A

这会给我这样的输出:

u_id    OverTime    TotalTime
-------------------------------------
1028    03:53:58    105:16:20

现在我的问题是我必须在zend框架中实现这个查询,因为我尝试过这样:

public function gettotallog()
{              
    $sql=$this->select()
           ->setIntegrityCheck(false)
           ->from(array('u'=>'logmaster'),array('id','project_id','u_id','insert_date',
                  "SEC_TO_TIME(SUM(CASE WHEN TotalTime > 32400
                   THEN TotalTime - 28800
                   ELSE 0
                   END)) as OverTime , SEC_TO_TIME(SUM(TotalTime)) AS TotalTime
                   FROM 
                   (
                   SUM( TIME_TO_SEC(
                   CASE WHEN endtime IS NULL
                   THEN timediff( starttime, starttime )
                   ELSE timediff( endtime, starttime )
                   END ) )  AS TotalTime                       
                   ) AS A"

               ))
           ->joinleft(array('a'=>'projectmaster'), 'u.project_id = a.id', array('pname'=>'name'))
           ->join(array('i'=>'usermaster'),'u.u_id=i.id',array('uname'=>'name')) 
           ->where("u.insert_date >='$yr-$mh-1' AND u.insert_date <='$yr-$mh-31'")    
           ->where("u.project_id!='NULL'")    
           ->where("u.u_id='$uid'")
           ->where('u.is_delete=false')
           ->group('u.u_id');   
       $resultSet = $this->fetchAll($sql);                     
       return $resultSet;
}

但它显示的错误是这样的:

  

异常信息:

     

消息:SQLSTATE [42000]:语法错误或访问冲突:1064您   您的SQL语法有错误;检查对应的手册   您的MySQL服务器版本,以便在'SUM附近使用正确的语法(   TIME_TO_SEC(第7行的“结束时间为空”的情况

有人可以帮我这个吗?

从不介意解决方案

public function gettotallog($uid,$yr,$mh)
{                                      
    $subsql=$this->select()
            ->setIntegrityCheck(false)
            ->from(array('u'=>'logmaster'),array('u_id','insert_date',
                "SUM( TIME_TO_SEC(
                CASE WHEN endtime IS NULL
                THEN timediff( starttime, starttime )
                ELSE timediff( endtime, starttime )
                END ) )  AS TotalTime"
                ))                
            ->join(array('i'=>'usermaster'),'u.u_id=i.id',array('uname'=>'name')) 
            ->where("u.insert_date >='$yr-$mh-1' AND u.insert_date <='$yr-$mh-31'")    
            ->where("u.project_id!='NULL'")    
            ->where("u.u_id='$uid'")
            ->where('u.is_delete=false')
            ->group('u.insert_date');


        $sql=$this->select()
            ->setIntegrityCheck(false)
            ->from(array('u'=>$subsql),array("u_id,uname,insert_date,SEC_TO_TIME(SUM(CASE WHEN TotalTime > 32400
                THEN TotalTime - 28800
                ELSE 0
                END)) as OverTime ,  SEC_TO_TIME(SUM(TotalTime)) AS TotalTime"                      
                ));
        $resultSet = $this->fetchAll($sql);                     
        return $resultSet;
}

1 个答案:

答案 0 :(得分:0)

public function gettotallog($uid,$yr,$mh)
{                                      
     $subsql=$this->select()
        ->setIntegrityCheck(false)
        ->from(array('u'=>'logmaster'),array('u_id','insert_date',
            "SUM( TIME_TO_SEC(
            CASE WHEN endtime IS NULL
            THEN timediff( starttime, starttime )
            ELSE timediff( endtime, starttime )
            END ) )  AS TotalTime"
            ))                
        ->join(array('i'=>'usermaster'),'u.u_id=i.id',array('uname'=>'name')) 
        ->where("u.insert_date >='$yr-$mh-1' AND u.insert_date <='$yr-$mh-31'")    
        ->where("u.project_id!='NULL'")    
        ->where("u.u_id='$uid'")
        ->where('u.is_delete=false')
        ->group('u.insert_date');


    $sql=$this->select()
        ->setIntegrityCheck(false)
        ->from(array('u'=>$subsql),array("u_id,uname,insert_date,SEC_TO_TIME(SUM(CASE WHEN TotalTime > 32400
            THEN TotalTime - 28800
            ELSE 0
            END)) as OverTime ,  SEC_TO_TIME(SUM(TotalTime)) AS TotalTime"                      
            ));
    $resultSet = $this->fetchAll($sql);                     
    return $resultSet;
}