mysql用户在null exisit的时间段内

时间:2017-02-07 08:50:53

标签: mysql timestamp

现在假设是2017-01-18 15:00:00

In                    Name            Out
2017-01-17 09:00:00   Sam             2017-01-17 11:00:00
2017-01-18 10:00:00   James           2017-01-18 12:00:00
2017-01-18 10:00:00   John            NULL          -> means still in office

如何在2017-01-18 11:00:00(开始范围)和2017-01-18 13:00:00(结束范围)之间找到所有在职人员?例如?

我不确定如何在搜索开始范围之后处理' In'时间和搜索结束范围在' Out(NULL)'时间。

我应该把詹姆斯和约翰作为答案。

试过这个(只回归詹姆斯):

SELECT * FROM TABLE WHERE 
(In BETWEEN '2017-01-18 10:00:00' AND '2017-01-18 13:00:00' OR Out BETWEEN 2017-01-18 10:00:00' AND '2017-01-18 13:00:00') 
OR 
('2017-01-18 10:00:00' BETWEEN In AND Out OR '2017-01-18 13:00:00' BETWEEN In AND Out)

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

SELECT *
FROM my_table
 WHERE '2017-01-18 10:00:00' < COALESCE(`Out`,NOW())
  AND '2017-01-18 13:00:00' >= `In`;