MySQL concat() to create column names to be used in a query?

前端 未结 5 596
走了就别回头了
走了就别回头了 2020-11-30 06:43

I would like to concatenate column names in a way that the first part of the column name is a string and the second part is a number which is the result of another query.

5条回答
  •  情歌与酒
    2020-11-30 07:41

    If the number of columns is fixed, then a non-dynamic approach could be:

    select 
      case mytable.mycolumn
        when 1 then column1  -- or: when 'a' then columna
        when 2 then column2
        when ...
        else ...
      end as my_semi_dynamic_column
    from ...
    

提交回复
热议问题