我试过,但失败:
mysql> select max(1,0);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0)' at line 1
我试过,但失败:
mysql> select max(1,0);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0)' at line 1
使用GREATEST()
例如:
SELECT GREATEST(2,1);
为了获得在一组行的列的最大值:
SELECT MAX(column1) FROM table; -- expect one result
为了得到一组列,文字,或为每行变量的最大值:
SELECT GREATEST(column1, 1, 0, @val) FROM table; -- expect many results
您可以使用GREATEST函数不能为空场。 如果这个值(或两者)中的一个可以为NULL,不使用它(结果可以为空)。
select
if(
fieldA is NULL,
if(fieldB is NULL, NULL, fieldB), /* second NULL is default value */
if(fieldB is NULL, field A, GREATEST(fieldA, fieldB))
) as maxValue
您可以更改NULL您的首选默认值(如果这两个值是NULL)。