Search code examples
mysqlfieldconditional-statementsfill

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.


Solution

  • 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.