这里的表格如下:
CREATE TABLE my_table
(
id INT,
user_id VARCHAR(5),
emp_id VARCHAR(5),
inst_id VARCHAR(5)
);
INSERT INTO my_table
VALUES
(1, 100, 3, 0),
(2, 200, 3, 0),
(3, 300, 3, 0),
(4, 400, 3, 0),
(5, 500, 3, 0),
(6, 600, 3, 0),
(7, 700, 3, 0),
(8, 800, 3, 0);
UPDATE my_table SET emp_id = 0, user_id = inst_id WHERE id IN (1,3,5,6)
how to update user_id values goes to inst_id ?
最后我应该回答这个问题
select * from my_table where id in (1,3,5,6)
答案 0 :(得分:1)
为了达到这样的要求,你需要使用UPDATE FROM语句:
UPDATE my_table
SET emp_id = 0, inst_id = user_id
FROM my_table
WHERE id IN (1,3,5,6)
<强>输出:强>
id user_id emp_id inst_id
1 100 0 100
2 200 3 0
3 300 0 300
4 400 3 0
5 500 0 500
6 600 0 600
7 700 3 0
8 800 3 0