Get column name instead of column value

后端 未结 3 1192
别跟我提以往
别跟我提以往 2021-01-24 05:19

My table table1 is like below

id   d1   d2   d3   d4
1    6    7    8    9
2    13   10   11   12
3    16   17   14   15
4    19   20   21   18
         


        
3条回答
  •  逝去的感伤
    2021-01-24 05:41

    You can do this with a case expression:

    SELECT d1, d2, d3, d4, least(d1, d2, d3, d4) as min_value,
           (CASE  least(d1, d2, d3, d4)
                WHEN d1 THEN 'd1'
                WHEN d2 THEN 'd2'
                WHEN d3 THEN 'd3'
                WHEN d4 THEN 'd4'
            END) as min_column_name
    FROM table1;
    

提交回复
热议问题