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 |NULL
I need to get the greatest value’s column name in a certain row. It should get:
id | maxcol ---+------- 1 | 2 2 | 2 3 | 1
Are there any queries that will do this? I’ve been trying, but I can’t get it to work right.
Are you looking for something like the
GREATESTfunction? For example:Combine it with a
CASEstatement to get column names:It’s not pretty. You’d do better to follow Bill Karwin’s suggestion and normalize, or simply take care of this in PHP.