表1:
id chapter sentence text
1 1 1 This is line1
2 1 2 This is line2
3 1 3 This is line3
4 1 4 This is line4
5 2 1 This is line1
6 2 2 This is line2
7 2 3 This is line3
8 2 4 This is line4
9 2 5 This is line5
10 2 6 This is line6
11 2 7 This is line7
12 3 1 This is line1
13 3 2 This is line2
14 3 3 This is line3
15 3 4 This is line4
16 3 5 This is line5
我正在尝试获取范围内的数据。我尝试了一些查询,但没有得到预期的结果。
GetData(beginingChapter, endingChapter, beginingSentence, endingSentence)
GetData(1, 3, 3, 4)
SQL查询:
SELECT *FROM table1 WHERE (chapter=1 AND sentence>=2) OR (chapter=3 AND sentence>= 1 AND sentence<= 4);
例外结果:
3 1 3 This is line3
4 1 4 This is line4
5 2 1 This is line1
6 2 2 This is line2
7 2 3 This is line3
8 2 4 This is line4
9 2 5 This is line5
10 2 6 This is line6
11 2 7 This is line7
12 3 1 This is line1
13 3 2 This is line2
14 3 3 This is line3
15 3 4 This is line4
有什么建议吗?如何达到预期效果?
答案 0 :(得分:1)
一种简单的方法是将查询结构化为所有与下限匹配的页面都有一个条件,一个条件与上限匹配,并简单地将它们组合在一起;
SELECT *
FROM table1
WHERE (( chapter = 1 AND sentence >= 3 ) OR chapter > 1)
AND (( chapter = 3 AND sentence <= 4 ) OR chapter < 3)
答案 1 :(得分:0)
根据:
GetData(beginingChapter, endingChapter, beginingSentence, endingSentence)
GetData(1, 3, 3, 4)
我会这样做:
SELECT *
FROM table1
WHERE
chapter BETWEEN 1 AND 3
AND
sentence BETWEEN 3 AND 4
但我不理解你的预期结果
答案 2 :(得分:0)
您的预期结果不明确,
根据我的理解,请尝试以下查询
SELECT * FROM table1 WHERE (chapter Between 1 AND 3) AND (sentence Between 3 AND 4);