5

我有一个mysql查询:

SELECT count(*) as `present_days` 
FROM  tbl_intime_status 
WHERE employee_status = 'Out' and 
      present_status = 'Full Day' and 
      date LIKE '%/"+month2+"/"+year1+"' and 
      employee_id="+ EmpId+

从这个查询我得到没有。现在的Full Day

我的数据库记录中有present_status= 'Half Day'& present_status = 'Full Day'

如何计算“全天”+“半天”?

4

7 回答 7

4

如果你想分开计数,你可以这样做

SELECT present_status, count(*) as `present_days` 
FROM  tbl_intime_status 
WHERE employee_status = 'Out' and 
      present_status IN ('Full Day','Half Day')
      date LIKE '%/"+month2+"/"+year1+"' and 
      employee_id="+ EmpId+
GROUP BY present_status

如果您想要两者的总数,请执行此操作

SELECT count(*) as `present_days` 
FROM  tbl_intime_status 
WHERE employee_status = 'Out' and 
      present_status IN ('Full Day','Half Day')
      date LIKE '%/"+month2+"/"+year1+"' and 
      employee_id="+ EmpId+
于 2012-08-27T06:38:09.230 回答
3

尝试,不要忘记转义该date列,因为它是保留字。

SELECT present_status, count(*) as `present_days` 
FROM   tbl_intime_status 
WHERE  employee_status = 'Out' and 
       present_status IN ('Full Day','Half Day')
       `date` LIKE '%/"+month2+"/"+year1+"' and 
       employee_id = " + EmpId + "
GROUP BY present_status
于 2012-08-27T06:41:21.947 回答
2

我想试试这个,看看它是否有效:

...

present_status in ('Full Day',  'Half Day' )

...

语法可以在这里找到:SQL IN Operator。

于 2012-08-27T06:41:55.387 回答
1

只需将其添加到您的 where 子句中,或者使用IN

...
WHERE present_status IN ('Full Day','Half Day')
...

或与OR

...
WHERE (present_status = 'Full Day' OR present_status = 'Half Day')
...
于 2012-08-27T06:36:47.563 回答
1

这是你想要的:

SELECT count(*) as present_days 
FROM tbl_intime_status 
WHERE employee_status = 'Out' 
and present_status IN ('Full Day', 'Half Day') 
and date LIKE '%/"+month2+"/"+year1+"' 
and employee_id="+ EmpId
于 2012-08-27T06:37:42.053 回答
0

这将使您将所有类型的计数拆分为一行:

SELECT SUM(IF(present_status  = 'Full Day', 1, 0)) AS full_present_days,
       SUM(IF(present_status  = 'Half Day', 1, 0)) AS half_present_days,
       COUNT(*) AS present_days
FROM  tbl_intime_status
WHERE employee_status = 'Out' and
      present_status IN('Full Day', 'Half Day') and
      date LIKE '%/"+month2+"/"+year1+"' and
      employee_id="+ EmpId+;
于 2012-08-27T06:57:49.710 回答
0
SELECT SUM(
   CASE present_status
     WHEN 'Full Day' THEN 1
     WHEN 'Half Day' THEN 0.5
     END
 ) as `present_days`  
FROM  tbl_intime_status  WHERE employee_status = 'Out'

希望这是您正在寻找的查询。

于 2012-08-27T07:04:11.477 回答