1

我正在 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 服务器版本相对应的手册,以在第 7 行的 'SUM( TIME_TO_SEC( CASE WHEN endtime IS NULL ' 附近使用正确的语法

任何人都可以帮我解决这个问题吗?

没关系得到解决方案

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;
}
4

1 回答 1

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;
}
于 2016-03-21T10:42:35.153 回答