我有一个表结构
ID Col_1 col_2 col_3 col_4
1 34 23 45 32
2 20 19 67 18
3 40 10 76 86
我在这里想要col_1,col_,col_3,col_4的最大值,所以我的输出看起来像
ID Col_1 col_2 col_3 col_4 max
1 34 23 45 32 45
2 20 19 67 18 67
3 40 10 76 86 86
我尝试使用
SELECT ID, MAX(col_1,col_2,col_3,col_4) as max
FROM demo
GROUP BY ID
任何帮助都会非常感激。
答案 0 :(得分:1)
您需要规范化表格结构。试试这个
select ID, max(Col_1) as max_value from
(
select ID, Col_1 from table
union all
select ID, Col_2 from table
union all
select ID, Col_3 from table
union all
select ID, Col_4 from table
) as t group by ID
答案 1 :(得分:1)
SELECT id, col1, col2, col3, col4,
GREATEST(col1, col2, col3, col4) AS mx FROM demo
答案 2 :(得分:1)
你可能会试试这个
SELECT ID, col_1, col_2, col_3, col_4,
GREATEST(col_1, col_2, col_3, col_4) AS max_value FROM table_name
答案 3 :(得分:0)