mysql:内部连接时重复行

vhipe2zx  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(360)

我是这个网站的新手。我正在为我用php和mysql创建的考勤系统寻找一个解决这个重复行问题的方法。
在这个系统中,每个用户可以在每个工作日执行签入和 checkout 操作。一些用户可以在轮班工作的一天内进行多次签入和 checkout ,忽略了延迟状态(签入时在上午9:05之后)和提前状态( checkout 时在下午5:00之前)。本系统的mysql数据库中有三个表。

用于存储签入数据的tbl\u checkin在数据库中包含以下内容:

ID     User_Id     Date          Time     Late_status          Comment
------------------------------------------------------------------------------------------
1     UGT001     2017-12-29     08:14:13                   No Comment
2     UGT002     2017-12-29     09:54:52     LATE          Comment Goes Here
3     UGT001     2017-12-30     09:16:34     LATE          Sending kids to school
4     UGT002     2017-12-30     08:21:04               
5     UGT003     2018-01-02     08:05:11                   
6     UGT003     2018-01-02     12:05:32                   Second Checkin

tbl\ U checkout,用于存储 checkout 数据:

ID     User_Id     Date          Time     Early_status          Comment
------------------------------------------------------------------------------------------
1     UGT001     2017-12-29     17:14:13                   
2     UGT002     2017-12-29     15:54:13     EARLY         Hospital Appointment
3     UGT001     2017-12-30     16:58:21     EARLY         Late for movie night
4     UGT002     2017-12-30     18:00:43               
5     UGT003     2018-01-02     10:30:44                   Break Time
6     UGT003     2018-01-02     18:04:45

tbl\u userlogin,用于存储用户数据(以及此处未显示的其他信息,如工作开始日期、结束日期、密码、持续时间、角色和用户状态):

ID     User_Id     Name          
------------------------------------------------------------------------------------------
1     UGT001     KEVIN MARTIN                 
2     UGT002     AHMAD BIN IBRAHIM  
3     UGT003     ALISYA PUTRI KARISMA

对于此问题,我使用此sql查询显示完整的考勤日志,其中显示用户名、用户id、日期、签入时间、 checkout 时间、签入注解和 checkout 注解,首先对最新条目(按日期)进行排序:

SELECT DISTINCT 
    b.name AS Name, 
    c.user_id AS User_ID , 
    c.date AS Date, 
    a.time As "Time In",
    c.time As "Time Out",
    a.comment AS "Comment In",
    c.comment AS "Comment Out" 
FROM 
tbl_checkout c 
LEFT JOIN tbl_checkin a ON c.date=a.date AND c.user_id=a.user_id 
INNER JOIN tbl_userlogin b ON c.user_id=b.user_id 
ORDER BY c.date DESC

下面是我应该显示的结果:

Name                    User_ID     Date         Time In     Time Out     Comment In               Comment Out
----------------------------------------------------------------------------------------------------------------------
ALISYA PUTRI KARISMA    UGT003    2018-01-02    12:05:32     18:04:45    Second Checkin           
ALISYA PUTRI KARISMA    UGT003    2018-01-02    08:05:11     10:30:44                            Break Time
KEVIN MARTIN            UGT001    2017-12-30    09:16:34     16:58:21    Sending kids to school   Late for movie night
AHMAD BIN IBRAHIM       UGT002    2017-12-30    08:21:04     18:00:43
AHMAD BIN IBRAHIM       UGT002    2017-12-29    09:54:52     15:54:13    Comment Goes Here        Hospital Appointment
KEVIN MARTIN            UGT001    2017-12-29    08:14:13     17:14:13    No Comment

但是,执行此sql查询将显示如下:

Name                    User_ID     Date         Time In     Time Out     Comment In               Comment Out
----------------------------------------------------------------------------------------------------------------------
ALISYA PUTRI KARISMA    UGT003    2018-01-02    12:05:32     10:30:44    Second Checkin           Break Time
ALISYA PUTRI KARISMA    UGT003    2018-01-02    08:05:11     18:04:45    
ALISYA PUTRI KARISMA    UGT003    2018-01-02    12:05:32     18:04:45    Second Checkin           
ALISYA PUTRI KARISMA    UGT003    2018-01-02    08:05:11     10:30:44                            Break Time
KEVIN MARTIN            UGT001    2017-12-30    09:16:34     16:58:21    Sending kids to school   Late for movie night
AHMAD BIN IBRAHIM       UGT002    2017-12-30    08:21:04     18:00:43
AHMAD BIN IBRAHIM       UGT002    2017-12-29    09:54:52     15:54:13    Comment Goes Here        Hospital Appointment
KEVIN MARTIN            UGT001    2017-12-29    08:14:13     17:14:13    No Comment

只需查看ugt003(该用户可以执行多个签入和 checkout /班次安排)从上面的结果来看,由于读取了不同的签入时间/注解和 checkout 时间/注解,因此重复了该结果。
有人能帮我解决考勤系统的问题吗?谢谢您。

wlp8pajw

wlp8pajw1#

问题是,您加入签入和 checkout 只使用 date 如果在一天中有多个,这还不足以匹配它们。它匹配每一个 inout 因为这一天会造成重复(甚至在同一天增加更多的情况下会产生乘法)。
尝试此查询:

SELECT
    b.name AS Name, 
    c.user_id AS User_ID , 
    c.date AS Date, 
    a.time As "Time In",
    c.time As "Time Out",
    a.comment AS "Comment In",
    c.comment AS "Comment Out"
FROM 
tbl_checkin a
LEFT JOIN tbl_checkout c ON c.date=a.date AND c.user_id=a.user_id AND c.Time >= a.Time
LEFT JOIN tbl_checkout co2 ON co2.user_id=c.user_id AND c.date=co2.date  AND co2.Time >= a.Time AND co2.Time < c.Time
INNER JOIN tbl_userlogin b ON c.user_id=b.user_id 

WHERE 
    co2.ID IS NULL

ORDER BY c.date DESC, a.Time, c.Time

相关问题