i need create csv file includes field, different elements should listed, based on condition each entry.
in example have table
id col1 col2 col3 1 false false true 2 true true true 3 true false true
the csv should this:
id,params "1","col3" "2","col1,col2,col3" "3","col1,col3"
i thought of this:
select id, concat( if(`col1`, 'col1,',''), if(`col2`, 'col2,',''), if(`col3`, 'col3','') ) `params` `table`
but problem is, work if last column (col3
) true, otherwise have trailing comma @ end.
is possible in mysql sql dialect?
use null
instead of empty string in if
conditions , use concat_ws
concatenate values.
select id, concat_ws(',',c1,c2,c3) params (select id, if(`col1`, 'col1',null) c1, if(`col2`, 'col2',null) c2, if(`col3`, 'col3',null) c3 t) x
or
select id, concat_ws(',',if(col1,'col1',null),if(col2,'col2',null),if(col3,'col3',null)) params t
Comments
Post a Comment