在SQLite3中将字符串比较为数字 [英] Compare strings as numbers in SQLite3
本文介绍了在SQLite3中将字符串比较为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在SQLite中有以下查询:
I have the following query in SQLite:
SELECT * FROM t1 ORDER BY t1.field
其中 t1.field
是包含数字的文本列。是否可以强迫SQLite将t1.field的值视为数字而不是字符串(不使用 ALTER TABLE
)?现在排序是一个纯字符串,所以10在2之前。
Where t1.field
is a text column containing numbers. Is it posible to force SQLite to consider the values of t1.field as numbers instead of strings (whithout doing ALTER TABLE
)? Right now the sort is a pure string one, so 10 goes before 2.
谢谢。
推荐答案
找到解决方案:
SELECT * FROM t1 ORDER BY t1.field + 0
+ 0
部分似乎强制转换为数字
The + 0
part seems to force conversion to number
这篇关于在SQLite3中将字符串比较为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文