简体   繁体   English

没有选择最大值

[英]Not selecting Max Value

在此处输入图片说明

I am using the query 我正在使用查询

select max(entry_no) from tbl_Invmaster

but its giving me ans 9 however the max value is 10. 但是它给了我ans 9,但是最大值是10。

You probably have the numbers in a VARCHAR column. 您可能在VARCHAR列中有数字。 Ordering in those fields is by alphabetcal order . 在这些字段中的排序是按字母顺序排序的 That way 9 is bigger than 10 . 这样9大于10 Explanation from the link: 链接说明:

To determine which of two strings comes first in alphabetical order, their first letters are compared. 为了确定两个字符串中的哪个按字母顺序排在最前面,将比较它们的第一个字母。 If they differ, then the string whose first letter comes earlier in the alphabet is the one which comes first in alphabetical order. 如果它们不同,则第一个字母在字母表中排在前面的字符串就是按字母顺序在前的字符串。 If the first letters are the same, then the second letters are compared, and so on. 如果第一个字母相同,则比较第二个字母,依此类推。 If a position is reached where one string has no more letters to compare while the other does, then the first (shorter) string is deemed to come first in alphabetical order. 如果到达一个位置,其中一个字符串没有更多字母可比较,而另一个字符串没有其他字母可比较,则第一个(较短)字符串被视为按字母顺序排在第一位。

Your best solution is not to store numbers in VARCHAR columns but instead use the appropriate type, eg INT . 最好的解决方案不是将数字存储在VARCHAR列中,而是使用适当的类型,例如INT That way your query would return the correct result. 这样,您的查询将返回正确的结果。

If that is not an option for you, you could CAST the column to an integer type. 如果那不是您的选择,则可以将列CAST转换为整数类型。 Eg in SQL Server you would write: 例如在SQL Server中,您将编写:

select max(CAST(entry_no AS INT)) from tbl_Invmaster
select max( to_number( entry_no )) from tbl_invmaster

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM