我有一张名为' checkin_checkout'存储用户的出勤日志记录和“用户”记录。存储有关用户的基本信息的表。
用户:
id | name | username | password
1 | Jack | jack123 | jackPwd
2 | John | John1234 | john!23
3 | Jill | Jill123 | jill123
checkin_checkout:
id | users_id | checkin_time | checkout_time | is_delayed_checkin | is_early_checkout | locked
1 | 1 | 2014-09-22 9:0| 2014-09-22 18:0| 0 | 0 0
2 | 2 | 2014-09-22 9:5| 2014-09-22 18:5| 0 | 0 0
3 | 1 | 2014-09-23 9:0| 2014-09-23 18:8| 0 | 0 0
4 | 2 | 2014-09-23 9:7| 2014-09-23 18:9| 0 | 0 0
5 | 3 | 2014-09-23 9:0| 2014-09-23 18:0| 0 | 0 0
要显示两个日期之间的出勤日志,例如2014-09-22至2014-09-23,视图如下:
Emp_Name | Checkin_date | Check-in_time | Checkout_date | Checkout_time
Jack | 2014-09-22 | 09:0 | 2014-09-22 | 18:0
Jack | 2014-09-23 | 09:0 | 2014-09-23 | 18:8
John | 2014-09-22 | 09:5 | 2014-09-22 | 18:5
John | 2014-09-23 | 09:7 | 2014-09-23 | 18:9
Jill | 2014-09-23 | 09:0 | 2014-09-23 | 18:0
如果在该特定日期范围内没有找到该用户的记录,则“缺席”#39;显示(仅当找到零记录时)
Jill | Absent | | |
如果是吉尔,还需要显示2014-09-22的记录,并显示一些适当的信息,如:
Jill | 2014-09-22 | N/A | 2014-09-22 | N/A (which is not displayed in current case)
Jill | 2014-09-23 | 09:0 | 2014-09-23 | 18:0
sql查询:
BEGIN
select fullname,DATE(checkin_time)
as checkin_date,TIME(checkin_time)
as checkin_time,DATE(checkout_time)
as checkout_date ,TIME(checkout_time)
as checkout_time,early_checkout_remarks,
delayed_checkin_remarks
from
(select
users.id,concat(users.firstname,' ',users.lastname)
as fullname,checkin_time,checkout_time,total_time_elapsed
as time_spent,early_checkout_remarks,delayed_checkin_remarks
from
users
LEFT JOIN checkin_checkout
ON users.id=checkin_checkout.users_id
AND DATE(checkin_time) between date(start_date)
AND date(end_date) WHERE users.deleted IS NULL
order by id,firstname)
as t1 group by checkin_date,fullname;
END
有什么建议吗?