Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql - fill / update field by other fields based on another field condition being true

I want to update all fields in a table based on another field having a condition being true, e.g.

Table1

field1 (string)

field2 (string)

field3 (condition to check)

field4 (field to update)

In table1, if field3 = "XYZ" then i would like field 4 to be updated with a string consisting of field1 & field2 ..

I've tried the following:
UPDATE table1 SET field4 = CONCAT(field1,field2)

Unfortunately this obviously replace all the field4 values and didn't do what I was looking for .. I've looked online for a better example of how I can accomplish this but no luck .. it's greek to me .. any help or direction is appreciated.

like image 469
calstatehomes Avatar asked Aug 02 '13 19:08

calstatehomes


1 Answers

Unless I'm misunderstanding you, you want to use a WHERE clause:

UPDATE table1
SET field4 = CONCAT(field1,field2)
WHERE field3 = "XYZ"

Here is some information on it.

like image 175
Tomcat Avatar answered Sep 21 '22 12:09

Tomcat