MySQL嵌套选择。第一次进入&午夜时分重叠的最后一个条目。怎么样?

时间:2017-12-06 08:59:09

标签: mysql sql

我有下表:

table

我创建了以下嵌套的SELECT查询。

SELECT 

(SELECT utl_dtstamp FROM tbl_user_time_log
WHERE user_id = 465605 AND utl_event = 'CLOCK IN'
ORDER BY utl_dtstamp ASC
LIMIT 1) AS 'First Entry',

(SELECT utl_dtstamp FROM tbl_user_time_log
WHERE user_id = 465605 AND utl_event = 'CLOCK OUT' 
ORDER BY utl_dtstamp DESC
LIMIT 1) AS 'Last Entry';

结果如下:

result

我无法弄清楚如果用户最终在午夜工作,如果通过user_id与user_id的最后一个条目一起选择此项目的第一个条目。

我还基于DAY(NOW())创建了这两个额外的SELECTS,但似乎如果用户在午夜之后进入新项目,DAY(NOW())将开始感知这个用户在午夜前进的第一个条目...

SELECT 

(SELECT utl_dtstamp FROM tbl_user_time_log
WHERE user_id = 465605 AND DAY(utl_dtstamp) = DAY(NOW()) 
ORDER BY utl_dtstamp ASC
LIMIT 1) AS 'First Entry';

***LAST stamp BY USER so far TODAY.

(SELECT utl_dtstamp FROM tbl_user_time_log
WHERE user_id = 465605 AND DAY(utl_dtstamp) = DAY(NOW()) 
ORDER BY utl_dtstamp DESC
LIMIT 1) AS 'Last Entry';

任何想法,指针,样本都会很棒。

0 个答案:

没有答案