需要通过连接2个表来进行更新查询

时间:2013-04-26 05:45:45

标签: php mysql

我需要帮助才能找到可能的解决方案。我坚持这个查询。

表1 - hs_hr_employee

  • emp_number
  • employee_id
  • emp_lastname
  • joined_date

表2 - ohrm_leave_entitlement

  • emp_number
  • no_of_days decimal(19,15)
  • days_used decimal(8,4)
  • days_used
  • leave_type_id

我需要加入这些表并获得加入日期并更新0.5添加。如果差异(当前dt-Dt。of Joining)> 10。

这是我的查询

 UPDATE ohrm_leave_entitlement
 SET no_of_days = no_of_days + 0.5
 WHERE 
 ( 
     ohrm_leave_entitlement.emp_number = hs_hr_employee.emp_number
     AND 
     hs_hr_employee.joined_date < DATE_SUB( NOW(), INTERVAL 10 DAY)
     AND
     leave_type_id=2
 ) 

但它不起作用..

3 个答案:

答案 0 :(得分:2)

UPDATE  ohrm_leave_entitlement a
        INNER JOIN hs_hr_employee b
            ON a.emp_number = b.emp_number
SET     a.leave_type_id = a.leave_type_id + 0.5
WHERE   DATEDIFF(NOW(), b.joined_date) > 10

答案 1 :(得分:0)

您可以尝试以下内容:

UPDATE ohrm_leave_entitlement , hs_hr_employee
SET ohrm_leave_entitlement.leave_type_id = ohrm_leave_entitlement.leave_type_id + 0.5
WHERE ohrm_leave_entitlement.emp_number = hs_hr_employee.emp_number
AND hs_hr_employee.joined_date < DATE_SUB( NOW(), INTERVAL 10 DAY)

答案 2 :(得分:0)

您可以使用:

   UPDATE ohrm_leave_entitlement
    SET leave_type_id = leave_type_id + 0.5
    WHERE  
     ohrm_leave_entitlement.emp_number IN (SELECT  hs_hr_employee.emp_number FROM hs_hr_employee 
    WHERE  hs_hr_employee.joined_date < DATE_SUB( NOW(), INTERVAL 10 DAY))