0

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

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

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

这里有2个场景。

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

案例 2: 给定数字1000, SELECT 应该返回记录 3

4

3 回答 3

1

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

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

SELECT * FROM mark WHERE x BETWEEN pointerMin AND pointerMax
于 2013-10-07T14:33:58.720 回答
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)

它使用LTRIMandRTRIM删除第一个或最后一个数字,然后做一些长度数学来计算子字符串索引。

它实际上也有效。

尽管如此,我还是强烈建议您重新考虑您的数据库设计。

于 2013-10-07T14:56:12.003 回答
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);

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

于 2013-10-07T14:35:54.340 回答