Ronald
Ronald

Reputation: 2917

Set column value to another column in the same row?

I have a table foo:

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

I need to update row with id 1 like this:

update foo set result = 'str1=x,str2=col2_value' where Id = 2

result after update must be:

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

How to put value of col2 to the string in result?

Upvotes: 0

Views: 147

Answers (3)

Mansoor
Mansoor

Reputation: 4192

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

Upvotes: 0

Kevin Kloet
Kevin Kloet

Reputation: 1086

Sounds like you need the concat function:

MySQL Concat

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

Upvotes: 0

Rumpelstinsk
Rumpelstinsk

Reputation: 3241

Just use Concat

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

Upvotes: 1

Related Questions