Select a specific column based on another column's value

前端 未结 4 1596
挽巷
挽巷 2021-02-12 22:05

I have a table like this

ID | Type | Val0 | Val1
1  |  0   |  A   | NULL
2  |  1   | NULL |  B

I need to select Val0 when the type

4条回答
  •  温柔的废话
    2021-02-12 23:09

    For low values of N, you can do it ad-hoc using the CASE statement, like CASE Type WHEN 0 THEN Val0 WHEN 1 THEN Val1 END. If your N is bigger, you should probably normalize your database (i.e. put ID => ValN mappings in a different table).

提交回复
热议问题