經過太多小時後,我無法找到答案。查找行中的最大值和回顯編號和列名
首先我的表
id | one | two | three | four | five | galname
-------------------------------------------------
1 | 2 | 5 | 23 | 4 | 5 | Bob
如何找到該行的最高值,並顯示colomun名。
three - 23
經過太多小時後,我無法找到答案。查找行中的最大值和回顯編號和列名
首先我的表
id | one | two | three | four | five | galname
-------------------------------------------------
1 | 2 | 5 | 23 | 4 | 5 | Bob
如何找到該行的最高值,並顯示colomun名。
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
我認爲你應該做的:
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? (我適應了答案從POST滿足您的需求,如果你有問題,反正是指該職位)
你爲什麼要張貼他人的答案?這是從上面評論中發佈的鏈接複製的。 – Daric
這正是我寫的!(我改編的答案適合你的需要意味着'如果你不想從其他答案中修改答案,我爲你做了,我想我很清楚) –
從那裏,你想獲得的信息(PHP的?) – Manuel
[規格化數據(http://stackoverflow.com/questions/246701/what-i s-normalization-or-normalization-why-it-it-important) – YXD
根據你想要做的事情,你的數據沒有被標準化 - 你不能在MySQL中輕鬆使用PIVOT。 – Konerak