How to get the column name of the result of a least function?

后端 未结 4 1036
北海茫月
北海茫月 2021-01-27 03:24

I have an employee table that looks like this:

| id | name | q1 | q2 | q3 | q4 |
+----+------+----+----+----+----+
| 1  | John | 20 | 30 | 10 | 4  |
| 2  | Ram           


        
4条回答
  •  醉酒成梦
    2021-01-27 04:17

    Use a "simple" or "switched" CASE statement:

    SELECT CASE LEAST(q1, q2, q3, q4)
              WHEN q1 THEN 'q1'
              WHEN q2 THEN 'q2'
              WHEN q3 THEN 'q3'
              ELSE 'q4'
           END as chosen_item
    

    In case of ties, the first item in the list will be reported.

提交回复
热议问题