将列值设置为同一行中的另一列?

时间:2016-10-14 07:28:31

标签: sql oracle

我有一张桌子foo:

Id col1 col2  result
-- ---- ---- -----
1   a     b 
2   c     d   

我需要更新ID为1的行:

update foo set result = 'str1=x,str2=col2_value' where Id = 2
更新后的

结果必须是:

Id col1 col2  result
-- ---- ---- ----------
1   a     b   str1=x,str2=b
2   c     d

如何将col2的值放入结果中的字符串?

3 个答案:

答案 0 :(得分:1)

只需使用Concat

即可
update foo set result=CONCAT('str1=x,str2=',col2) where Id=1

答案 1 :(得分:0)

听起来你需要concat函数:

MySQL Concat

UPDATE foo SET result = CONCAT('str1=x,str2=',col2) WHERE id = 1;

答案 2 :(得分:0)

UPDATE foo SET result ='str1 = x,str2 ='+ col2 +''FROM foo WHERE id = 1;