Find highest value in row and echo number and column name
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
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
I think you should do:
SELECT CASE GREATEST(`id`,`one`, `two`, `three`, `four`, `five`)
WHEN `id` THEN `id`
WHEN `one` THEN `one`
WHEN `two` THEN `two`
WHEN `three` THEN `three`
WHEN `four` THEN `four`
WHEN `five` THEN `five`
ELSE 0
END AS maxcol,
GREATEST(`id`,`one`, `two`, `three`, `four`, `five`) as maxvalue
FROM tbl
this is just if you don't want to look here: MySQL greatest value in row? (i adapted the answer FROM THAT POST to fit your needs if you have problems, anyway refer to that post)
精彩评论