如何将具有不同可能性的单个列的记录加起来?

时间:2012-08-27 06:33:55

标签: java mysql sql

我有一个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'

如何计算'全天'+'半天'?

7 个答案:

答案 0 :(得分: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+

答案 1 :(得分: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

答案 2 :(得分:2)

我想尝试一下,看它是否有效:

...

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

...

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

答案 3 :(得分:1)

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

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

OR

...
WHERE (present_status = 'Full Day' OR present_status = 'Half Day')
...

答案 4 :(得分: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

答案 5 :(得分: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+;

答案 6 :(得分: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'

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