2013-04-26 83 views
-1

我需要幫助來找出可能的解決方案。我陷入了這個查詢。需要通過加入2個表來更新查詢

表1 - hs_hr_employee

  • emp_number
  • EMPLOYEE_ID
  • emp_lastname
  • joined_date

表2 - ohrm_leave_entitlement

  • emp_number
  • NO_OF_DAYS十進制(19,15)
  • days_used十進制(8,4)
  • days_used
  • leave_type_id

我需要加入這些表格並獲得加入日期和更新0.5另外。如果差異(加入的當前時間)> 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 
) 

但其沒有工作..

+0

有什麼錯誤? – 2013-04-26 05:49:33

回答

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 
+0

感謝它的工作.. – 2013-04-26 06:15:26

+0

不客氣':)' – 2013-04-26 06:15:40

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