我有150名员工和他们的工资数据 我需要交换employee_id 120和121的工资 假设employee_id 120的工资是8000 employee_id 121的工资是8200 所以我需要交换id&#39的
的工资答案 0 :(得分:1)
如果您已经知道两位员工的最终工资,那么我认为您可以在一个UPDATE
查询中对这些值进行硬编码:
UPDATE yourTable
SET
salary = CASE WHEN employee_id = 120 THEN 8000 ELSE 8200 END
WHERE
employee_id IN (120, 121)
如果我读得正确的话,假设您希望员工120
的薪水为8000而121
的薪水为8200。
答案 1 :(得分:0)
示例数据: -
ID Sal
120 8000
121 2000
添加120和121的工资(比如8000和2000)并将其更新为120的新工资.120的工资为10000
UPDATE yourtable
SET sal=(SELECT sum(sal) from yourtable where id in (120,121))
WHERE ID = 120
ID Sal
120 10000
121 2000
现在将薪水121更新为薪水121和120
之间的差额UPDATE yourtable y
SET sal=(SELECT sal-y.sal from yourtable where id =120)
WHERE ID = 121
ID Sal
120 10000
121 8000
现在将薪水120更新为薪水121和120
之间的差额UPDATE yourtable y
SET sal=(SELECT y.sal-sal from yourtable where id =121)
WHERE ID = 120
ID Sal
120 2000
121 8000
答案 2 :(得分:0)
有了这两个id,这个简单的UPDATE
可以做到:
update tabename t1
set sal = (select sal from tablename t2 where t2.id = 241 - t1.id)
where t1.id in (120, 121)
对于id 120,从id 241 - 120 =>中选择薪水。 id 121的薪水。
对于id 121,从id 241 - 121 =>中选择薪水。 id 120的薪水。
答案 3 :(得分:0)
你也可以这样试试, 如果您再次遇到此类问题,则可以在需要更改emp_id的情况下使用它。
declare @str120sal varchar(25);
declare @str121sal varchar(25);
select @str120sal=salary from EmpTbl where employee_id=120;
select @str121sal=salary from EmpTbl where employee_id=121;
update EmpTbl set salary=@str120sal where employee_id=121;
update EmpTbl set salary=@str121sal where employee_id=120;
答案 4 :(得分:0)
另一种解决方案。
;WITH CTE AS (
SELECT *,
RN_ASC = ROW_NUMBER() OVER(ORDER BY employee_id),
RN_DSC = ROW_NUMBER() OVER(ORDER BY employee_id DESC)
FROM employees WHERE employee_id IN (120,121)
)
UPDATE T1
SET salary = T2.salary
FROM CTE T1 INNER JOIN CTE T2 ON T1.RN_ASC = T2.RN_DSC
答案 5 :(得分:0)
这是确切的答案
update e1 set e1.Salary = (select e2.salary from [EmployeeSalSwap] e2 where e2.id = (155- e1.Id) ) from TempOps.dbo.EmployeeSalSwap as e1 where Id in (75,80)
答案 6 :(得分:0)
CREATE TABLE salary (
emp_id int(11) NOT NULL,
salary float NOT NULL,
dept int(11) NOT NULL,
PRIMARY KEY (emp_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
INSERT INTO salary VALUES(100, 10000, 5),(101, 20000, 4),(103, 15000, 6),(104, 12000, 2)
UPDATE salary s1,salary s2 SET s1.salary = s2.salary, s2.salary = s1.salary WHERE s1.emp_id=100 AND s2.emp_id = 104