MySQL greatest value in row?

前端 未结 6 1882
日久生厌
日久生厌 2021-01-07 13:31

I\'m using MySQL with PHP. This is like my table: (I\'m using 3 values, but there are more)

id | 1 | 2 | 3
---+---+---+----
1  | 3 |12 |-29
2  | 5 |8  |8
3  | 99|7         


        
6条回答
  •  一个人的身影
    2021-01-07 13:52

    This is a great example of the way normalization helps make query design easier. In First Normal Form, you would create another table so all the values would be in one column, on separate rows.

    Since you have used repeating groups to store your values across three columns, you can find the column with the greatest value this way:

    SELECT id, IF(col1>col2 AND col1>col3, 'col1', IF(col2>col3, 'col2', 'col3')) 
      AS column_with_greatest_value
    FROM mytable;
    

提交回复
热议问题