MYSQL如果大于0,则选择Count

时间:2015-02-13 05:40:02

标签: java mysql

有没有办法可以计算最近大于0的所有数据?

Select *, timestampdiff(minute, t1.startTime, t.IOTime) as late from tbl_time GROUP BY name

如果迟到的回报率高于0则应计算。

Select *, COUNT(timestampdiff(minute, t1.startTime, t.IOTime)) as late from tbl_time

但我不知道如何使其成为条件

我的数据示例:

tbl_time
name     timeIn  date
Chris    9:50    2015-02-11    
Chris    9:30    2015-02-12    
Chris    9:20    2015-02-13    

在其他表中是startTime的位置

tbl_employee
name     startTime
Chris    9:25  

所以在时间戳中它会返回他已经晚了多少分钟我希望有一个返回值来计算返回大于0的所有时间戳,这样我可以计算他所有的总时间

name    total_late
Chris   2

这就是我的代码

rs = st.executeQuery("select name, SUM(late) as total from(SELECT name, CASE WHEN timestampdiff(minute, t1.startTime, t.timeIn)>0 THEN 1 END AS late FROM tbl_time )  t JOIN tbl_employee t1 ON t.name = t1.name GROUP BY name");

1 个答案:

答案 0 :(得分:0)

您可以使用CASE声明执行此操作。见下文:

SELECT name, SUM(late) AS total_late
(
SELECT B.name,
       CASE WHEN timestampdiff(minute, A.startTime, B.timeIn)>0 THEN 1 END AS late 
FROM tbl_employee A LEFT JOIN tbl_time B ON A.name=B.name
) AS Count_Late GROUP BY name