经过几个小时后,我找不到答案。
首先是我的表
id | one | two | three | four | five | galname
-------------------------------------------------
1 | 2 | 5 | 23 | 4 | 5 | Bob
如何在行中找到最高值并显示colomun名称。
three - 23
答案 0 :(得分:3)
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
答案 1 :(得分:1)
我认为你应该这样做:
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
这只是你不想看这里:MySQL greatest value in row? (如果你遇到问题,我会根据你的需要调整你的需求,无论如何都要参考那篇文章)