英文:
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);
英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论