由于这是https://t.me/tarantool和https://t.me/tarantoolru中最常见的问题,所以我在这里发布答案。
问问题
279 次
2 回答
2
space:pairs(from, 'GE'):
take_while(function(x) return x.field <= to end)
-- :totable() or use the result in the for-loop
于 2019-10-16T21:30:01.137 回答
1
您可以使用 Lua 和 SQL 来完成。
1) 在 Lua 中使用存储过程,如下所示:
function select_between(space_name, index_name, field_name, from, to)
local obj = index_name == nil and box.space[space_name] or box.space[space_name].index[index_name]
local result = {}
for _, tuple in obj:pairs(from, {iterator = 'GE'}) do
if (tuple[field_name] <= to) then
table.insert(result, tuple)
else
break
end
end
return result
end
select_between('test', nil, 'id', 1, 3)
2)从 Tarantool 2.0 开始,可以使用 SQL(前提是你有空格格式):
box.execute('select * from "test" where "id" between 1 and 3;')
于 2019-06-14T16:29:58.527 回答