0

我在 Python3 中使用 SQLite3 查询得到了意想不到的结果。

if first_waypoint_number < last_waypoint_number:
        result = list(c.execute("SELECT WPNumber, WPID, ROUTE FROM rte WHERE ROUTE = ? AND WPNumber BETWEEN ? AND ?", (route, first_waypoint_number, last_waypoint_number)))
        print(result)
else:
        result = list(c.execute("SELECT WPNumber, WPID, ROUTE FROM rte WHERE ROUTE = ? AND WPNumber BETWEEN ? AND ? ORDER BY WPNumber DESC", (route, first_waypoint_number, last_waypoint_number)))
        print(result)

第一个查询按预期工作,并产生以下结果:

[('A123', 25, 'WAYPOINTX'), ('A123', 26, 'WAYPONTY'), ('A123', 27, 'WAYPOINTZ')]

然而,第二个查询产生一个空列表[],而它应该以相反的顺序返回相同的列表。

我已经在 SQLite3 中测试了这两个查询,没有问题。

我在这里错过了什么吗?

4

1 回答 1

2

sqliteBETWEEN运算符等价于x>=y AND x<=z,因此当 时不匹配任何内容y > z

sqlite> create table foo (x int);
sqlite> insert into foo values (1);
sqlite> insert into foo values (2);
sqlite> insert into foo values (3);
sqlite> insert into foo values (4);
sqlite> select * from foo;
1
2
3
4
sqlite> select * from foo where x between 2 and 3;
2
3
sqlite> select * from foo where x between 3 and 2;
sqlite> 
于 2013-10-07T19:42:22.153 回答