SELECT列FROM表WHERE数字FALL-IN-RANGE-OF-TEXT

时间:2013-10-07 13:29:06

标签: sqlite android-sqlite

说,我们在表'mark'中有一个'指针'列。在该列中,我们有一些记录(TEXT)如下。

1. 0, 100, 200, 300, 400, 500
2. 500, 600, 700, 800, 900, 1000
3. 1000, 1100, 1200, 1300, 1400, 1500

有没有办法选择数字(int)落在范围内的记录(文本)?

这里有两种情况。

案例1: 给定数字 250 ,SELECT应返回记录1

案例2: 给定数字 1000 ,SELECT应返回 record 3

3 个答案:

答案 0 :(得分:1)

不可能使用SQLite的内置SQL函数从可变大小的列表中提取最后一个数字。

您应该更改表格以将最小值和最大值存储在单独的列中;然后你可以简单地做

SELECT * FROM mark WHERE x BETWEEN pointerMin AND pointerMax

答案 1 :(得分:1)

好的,这是迄今为止我写过的最疯狂的SQL。

SELECT pointer, 250 as X
FROM mark 
WHERE CAST(SUBSTR(pointer, 0, LENGTH(pointer) - LENGTH(LTRIM(pointer, "0123456789")) + 1) AS INT) <= X AND
X < CAST(SUBSTR(pointer, LENGTH(RTRIM(pointer, "0123456789"))) AS INT)

它使用LTRIMRTRIM删除第一个或最后一个数字,然后进行一些长度数学计算以找出子字符串索引。

它实际上也有效。

不过,我强烈建议您重新考虑数据库设计。

答案 2 :(得分:0)

缺少内置的向后INSTR会让这个查询变得疯狂!!!

对于正好6个字段,它就像是:

SELECT ROWID FROM mark WHERE 250 >= CAST(SUBSTR(pointer,1, INSTR(pointer, ",")-1) AS
INT) AND 250<CAST(SUBSTR(SUBSTR(SUBSTR(SUBSTR(SUBSTR(pointer,INSTR(pointer,",")+1),
INSTR(SUBSTR(pointer,INSTR(pointer,",")+1),",")+1), INSTR(SUBSTR(SUBSTR(pointer,
INSTR(pointer,",")+1),INSTR(SUBSTR(pointer,INSTR(pointer,",")+1),",")+1),",")+1),
INSTR(SUBSTR(SUBSTR(SUBSTR(pointer,INSTR(pointer,",")+1),INSTR(SUBSTR(pointer,
INSTR(pointer,",")+1),",")+1),INSTR(SUBSTR(SUBSTR(pointer,INSTR(pointer,",")+1),
INSTR(SUBSTR(pointer,INSTR(pointer,",")+1),",")+1),",")+1),",")+1),INSTR(SUBSTR(
SUBSTR(SUBSTR(SUBSTR(pointer,INSTR(pointer,",")+1),INSTR(SUBSTR(pointer,
INSTR(pointer,",")+1),",")+1),INSTR(SUBSTR(SUBSTR(pointer,INSTR(pointer,",")+1),
INSTR(SUBSTR(pointer,INSTR(pointer,",")+1),",")+1),",")+1),INSTR(SUBSTR(SUBSTR(
SUBSTR(pointer,INSTR(pointer,",")+1),INSTR(SUBSTR(pointer,INSTR(pointer,",")+1)
,",")+1),INSTR(SUBSTR(SUBSTR(pointer,INSTR(pointer,",")+1),INSTR(SUBSTR(pointer,
INSTR(pointer,",")+1),",")+1),",")+1),",")+1),",")+1) AS INT);

我建议你使用一个有问题的临时表计算INSTEAD ...

相关问题