简体   繁体   English

SQLite ORDER BY字符串,包含以0开头的数字

[英]SQLite ORDER BY string containing number starting with 0

as the title states: 如标题所述:

I have a select query, which I'm trying to "order by" a field which contains numbers, the thing is this numbers are really strings starting with 0s, so the "order by" is doing this... 我有一个选择查询,我正在尝试对包含数字的字段进行“排序”,问题是此数字实际上是以0开头的字符串,因此“ order by”正在执行此操作...

...
10
11
12
01
02
03
...

Any thoughts? 有什么想法吗?

EDIT: if I do this: "...ORDER BY (field+1)" I can workaround this, because somehow the string is internally being converted to integer. 编辑:如果我这样做:“ ... ORDER BY(field + 1)”我可以解决此问题,因为以某种方式将字符串在内部转换为整数。 Is this the a way to "officially" convert it like C's atoi? 这是像C的atoi一样“正式”转换它的方法吗?

You can use CAST http://www.sqlite.org/lang_expr.html#castexpr to cast the expression to an Integer. 您可以使用CAST http://www.sqlite.org/lang_expr.html#castexpr将表达式转换为Integer。

sqlite> CREATE TABLE T (value VARCHAR(2));
sqlite> INSERT INTO T (value) VALUES ('10');
sqlite> INSERT INTO T (value) VALUES ('11');
sqlite> INSERT INTO T (value) VALUES ('12');    
sqlite> INSERT INTO T (value) VALUES ('01');
sqlite> INSERT INTO T (value) VALUES ('02');
sqlite> INSERT INTO T (value) VALUES ('03');
sqlite> SELECT * FROM T ORDER BY CAST(value AS INTEGER);
01
02
03
10
11
12
sqlite>

if I do this: "...ORDER BY (field+1)" I can workaround this, because somehow the string is internally being converted to integer. 如果我这样做:“ ... ORDER BY(field + 1)”我可以解决此问题,因为以某种方式在内部将字符串转换为整数。 Is the a way to "officially" convert it like C's atoi? 是否可以像C的atoi一样“正式”转换它?

Well thats interesting, though I dont know how many DBMS support such an operation so I don't recommend it just in case you ever need to use a different system that doesn't support it, not to mention you are adding an extra operation, which can affect performance, though you also do this ORDER BY (field + 0) Im going to investigate the performance 挺有意思的,尽管我不知道有多少DBMS支持这样的操作,所以我不推荐这样做,以防万一您需要使用不支持该操作的其他系统,更不用说您要添加额外的操作,这可能会影响性能,尽管您也可以执行此操作ORDER BY (field + 0)我将调查性能

taken from the sqlite3 docs: 取自sqlite3文档:

A CAST expression is used to convert the value of to a different storage class in a similar way to the conversion that takes place when a column affinity is applied to a value. CAST表达式用于以类似于将列亲和力应用于值时进行的转换的方式将的值转换为不同的存储类。 Application of a CAST expression is different to application of a column affinity, as with a CAST expression the storage class conversion is forced even if it is lossy and irrreversible. CAST表达式的应用与列亲和力的应用不同,因为对于CAST表达式,存储类转换是强制性的,即使它是有损且不可逆的。

4.0 Operators 4.0运营商
All mathematical operators (+, -, *, /, %, <<, >>, &, and |) cast both operands to the NUMERIC storage class prior to being carried out. 所有数学运算符(+,-,*,/,%,<<,>>,&和|)都将两个操作数都强制转换为NUMERIC存储类。 The cast is carried through even if it is lossy and irreversible. 即使有损且不可逆转,也可以执行转换。 A NULL operand on a mathematical operator yields a NULL result. 数学运算符上的NULL操作数将产生NULL结果。 An operand on a mathematical operator that does not look in any way numeric and is not NULL is converted to 0 or 0.0. 数学运算符上的操作数不以任何方式显示,并且不为NULL,将转换为0或0.0。

I was curios so I ran some benchmarks: 我是好奇者,所以我运行了一些基准测试:

>>> setup = """
... import sqlite3
... import timeit
... 
... conn = sqlite3.connect(':memory:')
... c = conn.cursor()
... c.execute('CREATE TABLE T (value int)')
... for index in range(4000000, 0, -1):
...     _ = c.execute('INSERT INTO T (value) VALUES (%i)' % index)
... conn.commit()
... """
>>> 
>>> cast_conv = "result = c.execute('SELECT * FROM T ORDER BY CAST(value AS INTEGER)')"
>>> cast_affinity = "result = c.execute('SELECT * FROM T ORDER BY (value + 0)')"
>>> timeit.Timer(cast_conv, setup).timeit(number = 1)
18.145697116851807
>>> timeit.Timer(cast_affinity, setup).timeit(number = 1)
18.259973049163818
>>>

As we can see its a bit slower though not by much, interesting. 正如我们所看到的那样,它的运行速度虽然慢了一点,但并不有趣。

您可以使用CAST

ORDER BY CAST(columnname AS INTEGER)

In ListView with cursor loader! 在带有光标加载器的ListView中!

String projection= some string column;
String selection= need to select;

String sort="CAST ("+ YOUR_COLUMN_NAME + " AS INTEGER)";

CursorLoader(getActivity(), Table.CONTENT_URI, projection, selection, selectionArgs, sort);

Thanks to Skinnynerd . 感谢Skinnynerd with Kotlin, CAST worked as follows: CAST fix the problems of prioritizing 9 over 10 OR 22 over 206. 与Kotlin一起,CAST的工作方式如下:CAST解决了将9优先于10或将22优先于206的问题。

define global variable to alter later on demand, and then plug it in the query: 定义全局变量以在以后按需更改,然后将其插入查询中:

var SortOrder:String?=null

to alter the order use: 更改订单使用:

For descendant: 对于后代:

 SortOrder = "CAST(MyNumber AS INTEGER)" + " DESC"

(from highest to lowest) (从最高到最低)

For ascending: 对于升序:

 SortOrder =  "CAST(MyNumber AS INTEGER)" + " ASC"

(from lowest to highest) (从最低到最高)

CONVERT CAST function using order by column value number format in SQL SERVER 在SQL SERVER中使用按列值数字格式排序的CAST函数

SELECT * FROM Table_Name ORDER BY CAST(COLUMNNAME AS INT); SELECT * FROM Table_Name ORDER BY CAST(COLUMNNAME AS INT);

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

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