Mysql将5列合并为一列并替换行

时间:2012-05-26 19:22:18

标签: mysql str-replace

所以我有疑问:

SELECT worker_ID as widp, DAYS, MAX(ID_WERSJI) max , funcion1 as FUN1,function2 as FUN2,function3 as FUN3,function4 as FUN4,function5 as FUN5
                FROM `grafik201105`
                    WHERE 
                        funcion1 IN ('one','two','three','four','five','six') OR
                        function2 IN ('one','two','three','four','five','six') OR
                        function3 IN ('one','two','three','four','five','six') OR
                        function4 IN ('one','two','three','four','five','six') OR
                        function5 IN ('one','two','three','four','five','six') AND
                        active=1 
                        GROUP BY widp, FUN1,DAYS) as dni

现在我需要在一列('give_me_all')中获得fun1,fun2,fun3,fun4和fun5 和!在fun1 ='one'中将'one'替换为'first',如果'two'则将second替换为'first'= 3第三...

我尝试使用if(fun1 ='one','first',fun1),但我认为这里更好的解决方案是使用CASE但我不确切知道如何使用它

1 个答案:

答案 0 :(得分:1)

将两个或多个列组合成一个只需执行(function1+function2+function3+function4+function5) AS give_me_all,但正如我所说,最好的做法是将one替换为first然后它会像(change(function1)+change(function2)+change(function3)+change(function4)+change(function5)) AS give_me_all一样,但写一个函数是一个更高级的东西。尝试阅读更多here。 否则它看起来很难看,但仍然可能

`((CASE 
     function1 
        WHEN 'one' THEN 'first';
        WHEN 'two' THEN 'second';
  END;)+
  (CASE 
     function2 
        WHEN 'one' THEN 'first';
        WHEN 'two' THEN 'second';
  END;)
  ...) AS give_me_all`

看看它有多糟糕?更好地学会编写自己的函数:)