我想对下表使用更新命令。我想要做的是根据answer
列更新reference
列:第二和第三个答案应为A,因为它们的引用为1,第一行的答案为A,类似于5和6应该是X。
示例数据:
id answer ref
-------------------
1 A 1
2 B 1
3 C 1
4 x 4
5 y 4
6 z 4
答案 0 :(得分:2)
update a
set a.answer = b.answer
from tablename a, tablename b
where a.ref = b.id
编辑: 添加这个也可能更好;
and a.ref <> a.id
答案 1 :(得分:1)
update table1 t1 set answer = t2.answer from table1 t2 where t1.ref = t2.id;
答案 2 :(得分:0)
您可以使用case
声明
UPDATE sample_data
SET answer = ref
WHERE CASE
WHEN ref = 1
THEN answer = 'A'
ELSE
WHEN REF = 4
THEN ANSWER = 'x'
END as available