简体   繁体   English

根据日期从另一个表更新mysql一个表

[英]Update mysql one table from another table based on dates

I have a program for student attendance system and record save on mysql table like below if present then 1 and if absent then 0 我有一个学生出勤系统的程序,并记录保存在mysql表中,如下所示(如果存在则为1,如果不存在则为0)

Table -> attendance 表格->出勤

uid  date       status  App
1   01/07/2013  1   
1   01/07/2013  1   
1   01/07/2013  1   
1   01/07/2013  0   
1   01/07/2013  0   
1   02/07/2013  1   
1   02/07/2013  0   
1   02/07/2013  1   
1   02/07/2013  1   
1   02/07/2013  1   
1   03/07/2013  0   
1   03/07/2013  0   
1   03/07/2013  1   
1   03/07/2013  1   
1   03/07/2013  1   
1   04/07/2013  0   
1   04/07/2013  1   
1   04/07/2013  1   
1   04/07/2013  1   
1   04/07/2013  1   

And i also have a table where student submit their leave applications like below 我还有一张桌子,学生可以在这里提交休假申请,如下所示

Table -> application 表->应用程序

 id  uid    from            to           status
 1   1    04/07/2013    07/07/2013  approved
 2   1    11/07/2013    12/07/2013  rejected

I want that if status sets to approved then the date range given in application from 4 july to 7 july will be searched on "attendance" table and whatever the dates will be found with 0 status it will add 1 to "app" column and ignore the dates on which attendance is not taken. 我希望如果状态设置为“已批准”,那么将在“出勤”表中搜索7月4日至7月7日申请中给定的日期范围,无论状态为0的日期将为“ app”列加1并忽略不参加会议的日期。

UPDATE attendance  d
  JOIN application p
    ON d.uid = p.uid
   AND d.date BETWEEN p.from AND p.to
SET    d.status = 1
WHERE  p.status = 'approved'
UPDATE attendance  d
  JOIN application p
    ON d.uid = p.uid
   AND d.date BETWEEN p.from AND p.to
   AND p.status = 'approved'
SET    d.app = 1
WHERE  d.status = 0

This should do what you want: 这应该做您想要的:

UPDATE attendance a
    Set a.app=1
    WHERE a.uid IN
        (select z.uid
            FROM application z
            WHERE z.status='approved'
                AND a.date BETWEEN z.from and z.to);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM