MySQL greatest value in row?

前端 未结 6 1881
日久生厌
日久生厌 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 14:09

    In the php side, you could do something like this:

    foreach ($rows as $key => $row) {
      $bestCol = $best = -99999;
      foreach ($row as $col => $value) {
        if ($col == 'id') continue; // skip ID column
        if ($value > $best) {
          $bestcol = $col;
          $best = $value;
        }
      }
      $rows[$key]['best'] = $bestCol;
    }
    

    Or something similar...

提交回复
热议问题