Select a specific column based on another column's value

前端 未结 4 1595
挽巷
挽巷 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:03

    The way I read this, you need to use UNION:

    SELECT a.val0
      FROM TABLE a
     WHERE a.type = 0
    UNION ALL
    SELECT a.val1
      FROM TABLE a
     WHERE a.type = 1
    UNION ALL ...
    

    UNION ALL doesn't remove duplicates, and is faster than UNION (because it removes duplicates).

    Doing this dynamically is possible.

提交回复
热议问题