计算表的2列并根据表列的不同值更新第3列

时间:2016-11-03 16:56:11

标签: sql sql-server

update tbl_addemployee set 
amount_due=amount_due+((tsalary/30)*187),
com_salary_date='2016-11-04' 
where (forUser='sun4269' and E_id)

我想为每个不同的记录更新com_salary_date,其中forUser ='sun4269' 并且不同的列名称是E_id

1 个答案:

答案 0 :(得分:0)

您需要使用INNER JOIN

加入匹配的不同记录
UPDATE
    T
SET
    T.amount_due = amount_due+((tsalary/30)*187),
    T.com_salary_date = '2016-11-04' 
FROM
    tbl_addemployee  AS T
    INNER JOIN 
    (
        SELECT DISTINCT 
           forUser, 
           E_id 
         FROM 
           tbl_addemployee
    )  AS X
    ON X.forUser=T.forUser AND X.E_id=T.E_id