任何人都可以通过max来帮助我UPDATE吗?
表-A
EMP SALARY BONUS
A 100 110
B 50 80
C 30 20
D 80 50
E 30 40
我想要答案
表-A
EMP SALARY BONUS MAX
A 100 110 110
B 50 80 80
C 30 20 30
D 80 50 80
E 30 40 40
答案 0 :(得分:2)
您可以使用GREATEST
(MySQL):
UPDATE tableA
SET `max` = GREATEST(Salary, Bonus);
我建议添加计算列以避免将来更新:
CREATE TABLE tableA(Emp INT, Salary INT, BONUS INT,
`max` INT AS (GREATEST(Salary, Bonus)));
<强> Rextester Demo 强>
答案 1 :(得分:0)
SQL Server,您可以使用CASE STATEMENT
select
EMP,
SALARY,
BONUS,
[Max] = case when Salary > Bonus then Salary else Bonus end
from
TABLE-A
或者更新......
UPDATE TABLE-A
SET [Max] = case when Salary > Bonus then Salary else Bonus end
答案 2 :(得分:0)
对于SQL Server 2012 +:
UPDATE TABLE_A
SET [MAX] = IIF (SALARY > BONUS, SALARY, BONUS);