如何在6列中找到最大值

时间:2012-11-15 13:39:33

标签: php mysql

我有一张这样的桌子。

+-------------+-------+-------+-------+-------+-------+
| studentCode | term1 | term2 | term3 | term4 | term6 |
+-------------+-------+-------+-------+-------+-------+

我想返回每行中术语的最大值。

像这样:

SELECT studentCode, maxterm FROM my_tbl

如何用我的最大值填充maxterm 感谢...

4 个答案:

答案 0 :(得分:2)

尝试:

SELECT studentCode, GREATEST(term1, term2, term3, term4, ... ) FROM my_tbl

Source

答案 1 :(得分:1)

使用GREATEST()

select studentCode,
       greatest(term1, term2, term3, term4, term5, term6)
from my_tbl;

答案 2 :(得分:0)

只有几列,你可以使用if子句。

SELECT IF(`col_1` > `col_2`, `col_1`, `col_2`) AS max FROM `my_table`

答案 3 :(得分:0)

尝试使用GREATEST:

SELECT studentCode, GREATEST( term1, term2, term3, term4, term5, term6 )
FROM my_tbl