使用mysql从表中输入小时数

时间:2016-11-10 08:04:30

标签: mysql mysqli

这个查询:

如何获取三个选择查询的值   名字,小时进入',小时批准,小时被拒绝

   SELECT e.ENAME 'NAME', SUM(l.HOURS) 'HOURS ENTERED' FROM labors l, emps e 
WHERE l.EmpsID = e.EmpsID AND APPROVED = 'N' AND l.EmpsID = 'AGIUCL01'
AND l.DATE_WORK BETWEEN '2016-10-30' AND '2016-11-05' 
  union 
 (SELECT e.ENAME 'NAME', SUM(l.HOURS) 'HOURS APPROVED' FROM labors l, emps e
  WHERE l.EmpsID = e.EmpsID AND APPROVED = 'A' AND l.EmpsID = 'AGIUCL01' 
   AND l.DATE_WORK BETWEEN '2016-10-30' AND '2016-11-05') 
   union 
    SELECT e.ENAME 'NAME', SUM(l.HOURS) 'HOURS REJECTED' FROM labors l,empse 
    WHERE l.EmpsID = e.EmpsID AND APPROVED = 'R' 
    AND l.EmpsID = 'AGIUCL01' 
    AND l.DATE_WORK BETWEEN '2016-10-30' AND '2016-11-05'

查询:

SELECT e.ENAME 'NAME', 
SUM(CASE WHEN l.APPROVED='N' THEN l.HOURS ELSE 0 END) 'HOURS ENTERED',
SUM(CASE WHEN l.APPROVED='A' THEN l.HOURS ELSE 0 END) 'HOURS APPROVED', 
SUM(CASE WHEN l.APPROVED='R' THEN l.HOURS ELSE 0 END) 'HOURS REJECTED' 
FROM labors l ,emps e 
where l.DATE_WORK BETWEEN '2016-10-30' AND '2016-11-05'  and 
e.TERM_DATE < e.REHIRE_DATE 
or e.TERM_DATE IS NULL  
 group by (e.empsid)

我正在执行查询,表中的结果是这个。

   NAME                  HOURS ENTERED    HOURS APPROVED    HOURS REJECTED   
CLAUDINEAGIUS                292.00            39.00            20.00
TODD MICHAEL ALDAMA           292.00           39.00            20.00
STEVE  ALMGREN                292.00           39.00            20.00
SHAWN  ANDERSON                292.00          39.00            20.00
JARETT  BARNETT                292.00          39.00            20.00

但我没有让每个用户进入当前时间线。

1 个答案:

答案 0 :(得分:0)

您可以在SUM

中使用条件
SELECT e.ENAME 'NAME',
  SUM(CASE WHEN APPROVED='N' THEN l.HOURS ELSE 0 END) 'HOURS ENTERED',
  SUM(CASE WHEN APPROVED='A' THEN l.HOURS ELSE 0 END) 'HOURS APPROVED',
  SUM(CASE WHEN APPROVED='R' THEN l.HOURS ELSE 0 END) 'HOURS REJECTED'
FROM labors l, emps e 
WHERE l.EmpsID = e.EmpsID
  AND l.EmpsID = 'AGIUCL01'
  AND l.DATE_WORK BETWEEN '2016-10-30' AND '2016-11-05'