你可以使用MySQL的Greatest()函数来返回最大的数值。

huangapple go评论56阅读模式
英文:

How can I use MySQL Greatest() function to return the greatest numeric value?

问题

可以使用MySQL的GREATEST()函数来返回一组值中的最大数值,即使这组值中包含字符串值。根据我的了解,GREATEST()函数会返回最大的,即使它是一个字符串值。例如,下面的查询将返回"N/A"而不是"3":

SELECT GREATEST(1, 2, 3, "N/A");
--> "N/A"

在上面的示例中,是否有一种方法或技巧可以获取值"3"而不是"N/A"呢?

英文:

Is it possible to use MySQL's GREATEST() function to return the largest numeric value in a set, even if there's string values in the set?

As far as I know the GREATEST() function returns the greatest value, even if it's a string value. For example, the query below returns "N/A" and not "3":

SELECT GREATEST(1, 2, 3, "N/A");
--> "N/A"

Is there a method or trick where I can get the value "3" instead in the example set above?

答案1

得分: 1

The value 3 is returned, but since one of the arguments is a string, the value returned by GREATEST() does not have an integer type.

You can fix it in any of the following ways:

SELECT CAST(GREATEST(1, 2, 3, 'N/A') AS SIGNED);

SELECT GREATEST(1, 2, 3, CAST('N/A' AS SIGNED));

SELECT GREATEST(1, 2, 3, 'N/A'+0);

DBFiddle

英文:

The value 3 is returned, but since one of the arguments is a string, the value returned by GREATEST() does not have an integer type.

You can fix it in any of the following ways:

SELECT CAST(GREATEST(1, 2, 3, 'N/A') AS SIGNED);

SELECT GREATEST(1, 2, 3, CAST('N/A' AS SIGNED));

SELECT GREATEST(1, 2, 3, 'N/A'+0);

DBFiddle

huangapple
  • 本文由 发表于 2023年2月24日 00:45:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547814.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定