Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find highest value in row and echo number and column name

Tags:

mysql

max

row

After too many hours I can't find the answer to this.

First my table

  id | one | two | three | four | five | galname
-------------------------------------------------
  1  |  2  |  5  |   23  |  4   | 5    |  Bob

How do I find the highest value in the row and show colomun name.

three - 23
like image 989
Darren Avatar asked Oct 11 '22 16:10

Darren


1 Answers

 select  id,  GREATEST(one, two, three, four, five) value,
        case GREATEST(one, two, three, four, five)
         when one then 'one'
         when two then 'two'
         when three then 'three'
         when four then 'four'
         when five then 'five' end column_name
 from your_table        
like image 119
Michael Pakhantsov Avatar answered Oct 14 '22 00:10

Michael Pakhantsov