Rahul
Rahul

Reputation: 5834

How can I update a column in mysql table by inserting combination of text and values from other columns if its not null?

I have a table that contains 3 columns whose values can be null. I have to create a 4th column and concat value of only those columns that are not null. Expected Output:

col1   col2   col3   col4
----------------------------------------------------
1       0      1     col1=True;col2=False;col3=True;
0      Null    1     col1=False;col3=True

Upvotes: 1

Views: 31

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269643

Use concat_ws():

select concat_ws(';',
                 concat('col1=', elt(col1 + 1, 'false', 'true')),
                 concat('col2=', elt(col2 + 1, 'false', 'true')),
                 concat('col3=', elt(col3 + 1, 'false', 'true'))
                )
from t;

Upvotes: 1

Delly Fofie
Delly Fofie

Reputation: 314

The simplest approach is to write a script in the language of you choice in order to update the existing data.

And then include this routine in your application.

Upvotes: 0

Related Questions