SQL 字符串大于或小于

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6164498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:47:25  来源:igfitidea点击:

String greater than or less than

sql

提问by mwok

I have the following data:

我有以下数据:

[sequences]
/a1
/a2
/a3
...
/a10

The query SELECT * FROM sequences WHERE nbr <= '/a10'should return the list above, instead it returns:

查询SELECT * FROM sequences WHERE nbr <= '/a10'应该返回上面的列表,而是返回:

[results]
/a1
/a10

How do I make it return all the rows in the above list?

如何让它返回上面列表中的所有行?

回答by GolezTrol

It works as it should. To compare the numeric value, you'll have to convert these to numbers somehow. A good start would be to use substr(yourfieldname, 3) to cut of the/a. Then you can useconvert` to typecast it to int, so your final query will look something like:

它可以正常工作。要比较数值,您必须以某种方式将它们转换为数字。一个好的开始是使用substr(yourfieldname, 3) to cut of the/a . Then you can useconvert` 将其类型转换为 int,因此您的最终查询将类似于:

select * from sequences where convert(int, substr(nbr, 3)) <= 10

Mind that the exact functions and rules for converting strings to ints may very per dbms. This illustrates the general idea, though.

请注意,将字符串转换为整数的确切功能和规则可能非常适合每个 dbms。不过,这说明了总体思路。

回答by user unknown

SELECT *
FROM sequences 
WHERE toInt(substring (nbr, 2)) <= 10;

Name and syntax of 'substring'-function and 'toInt' will vary from db-implementation to db-implementation.

'substring'-function 和 'toInt' 的名称和语法会因 db-implementation 到 db-implementation 的不同而不同。