Aquaholic
Aquaholic

Reputation: 931

MySQL Update concat field value only if it does not contain a string

I have a table with following columns (among others).

brand varchar(15)
descr varchar(100)

Requirement is to check existing string that exists in "descr" and if that DOES NOT contain the value of the brand column, then append to it

For instance, if brand has value of "vanilla" and descr has value of "choco", then after update desc should become "chocovanilla"

if brand has value of "choco" and descr has value of "choco" or "chocovanilla", then after update nothing should change as desc already has what is there in brand.

I have the following code prototype, but unable to get it working.

update tbl set descr= concat(descr,'value from brand col') where descr not contain 'value from brand col';

Please let me know if this can be tweaked to fit the needs.

I saw this thread but it does not fit: MySQL update field in row if it doesn't contain value

Upvotes: 2

Views: 2467

Answers (2)

forpas
forpas

Reputation: 164214

You need like:

update tbl set descr = concat(descr,brand) where descr not like concat('%', brand, '%');

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1271151

You can use like and concat():

update tbl
    set descr = concat(descr, ' ', brand)
    where descr not like concat('%', brand, '%');

This adds a space before the brand for readability.

Upvotes: 6

Related Questions