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

时间:2013-07-12 13:34:15

标签: php mysql sql-update

我有一个学生考勤系统的程序和记录保存在mysql表上,如果存在则为1,如果不存在则为0

表 - >出勤

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   

我还有一张学生提交假期申请表的表格,如下所示

表 - >应用

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

我希望如果状态设置为已批准,那么将在7月4日到7月7日的应用程序中给出的日期范围将在“出勤”表中进行搜索,无论日期将在0状态下找到,它将向“app”添加1列,并忽略未参加的日期。

3 个答案:

答案 0 :(得分:3)

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'

答案 1 :(得分:3)

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

答案 2 :(得分:0)

这应该做你想要的:

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);