Trying to find a way to combine CONCAT with IF – or in some other way display textual value for set flags in a comma separated list.
Note: This is eventually for a table with 10+ columns. Using two here to simplify.
Say I have a table with flags. If flag is 1 I want to display some textual value,
else nothing.
origin:
+--+-----+-----+
|# | CHS | ACC |
+--+-----+-----+
|1 | 0 | 1 |
|2 | 1 | 1 |
|3 | 1 | 0 |
|4 | 0 | 0 |
+--+-----+-----+
I want:
+--+----------+
|# | origin |
+--+----------+
|1 | ACC |
|2 | CHS, ACC |
|3 | CHS |
|4 | |
+--+----------+
Not (this or the like):
+--+-----------+
|# | origin |
+--+-----------+
|1 | ACC, |
|2 | CHS, ACC, |
|3 | CHS, |
|4 | , |
+--+-----------+
Something in the direction of this:
SELECT
CONCAT_WS(', ',
IF(CHS = 0, '', 'CHS'),
IF(ACC = 0, '', 'ACC')
) as origin
FROM
origin;
But not with comma between empty values.
This is eventually for a table with 10 columns that I join with other table based on id.