159

I want to update the code on all my record to what they currently are plus _standard any ideas?

So for example if the codes are apple_1 and apple_2 I need them to be apple_1_standard and apple_2_standard

Before:

id   code
------------
1    apple_1 
1    apple_2

Psuedo Query:

update categories set code = code + "_standard" where id = 1;

Expected result:

id   code
----------------------
1    apple_1_standard 
1    apple_2_standard
1

2 Answers 2

322

You need to use the CONCAT() function in MySQL for string concatenation:

UPDATE categories SET code = CONCAT(code, '_standard') WHERE id = 1;
Sign up to request clarification or add additional context in comments.

4 Comments

Just in case somebody runs into the same issue as I did: If the field code is NULL by default you need to use: UPDATE categories SET code = CONCAT(IFNULL(code,''), '_standard') WHERE id = 1; Otherwise the concat will always result in NULL.
Alternatively you can use CONCAT_WS which skips NULL values. For example SELECT CONCAT_WS(', ','First name',NULL,'Last Name'); gives 'First name, Last Name'
@Daniel How about updating the answer according to comments above?
special/separate question for possible null case : stackoverflow.com/questions/14020867/…
15

Update image field to add full URL, ignoring null fields:

UPDATE test SET image = CONCAT('https://my-site.com/images/',image) WHERE image IS NOT NULL;

1 Comment

Whats funny is even though this doesn't answer the OP question very well, this is exactly what I needed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.