SQLite 循环语句?
SQLite 中是否有任何循环语句,例如 FOR .. in .. LOOP
或类似的语句?我有两列 StartRange、EndRange,我需要在另一个表中插入整个序列。因此,如果 StartRange
为 1 并且 EndRange
为 3,则需要使用值 1、2、3
进行三个插入。
Is there any loop statements in SQLite like FOR .. in .. LOOP
or something like that? I have two columns StartRange, EndRange
and I need to insert a whole sequence in the other table. So if StartRange
is 1 and EndRange
is 3 it's necessary to make three inserts with the values 1, 2, 3
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
显然,SQLite 中的循环结构是 WITH RECURSIVE 子句。
该文档链接包含从数到十的示例代码、Mandelbrot 集绘图仪和数独谜题求解器,全部采用纯 SQL 语言。
这是一个计算斐波那契数列的 SQLite 查询,让您感受一下:
这是一个 埃拉托斯特尼筛法:
Apparently the looping construct in SQLite is the WITH RECURSIVE clause.
That documentation link has sample count-to-ten code, a Mandelbrot set plotter, and a Sudoku puzzle solver, all in pure SQL.
Here's an SQLite query that computes the Fibonacci sequence to give you a feel for it:
And here's a Sieve of Eratosthenes:
您可以使用递归触发器在 SQL 中创建循环。使用 mu 太短 的模式,
我们需要在 SQLite 中启用递归触发器:
创建一个临时触发器以循环到范围的末尾:
启动它:
You can make loops in SQL with recursive triggers. Using mu is too short's schema
we need to enable recursive triggers in SQLite:
Make a temporary trigger to loop up to the end of the range:
Kick it off:
如果您有一个额外的表来保存您需要的所有整数,则可以直接使用 SQL 执行此类操作。
假设您的
StartRange
和EndRange
范围在 1 到 10 之间,并且您有一个如下所示的表:该表仅包含您需要的所有可能的整数(即 1 到 10)。
然后,如果您也有这个:
您可以使用连接将 INSERT 插入到
target
中:结果是这样的:
当然,您真正的
t
会有更多行,因此您可以想要一个 WHERE 子句来限制您查看的t
行。类似的事情经常用日期来完成(查找“日历表”)。
因此,如果您的范围很小(对于小的某些定义),则生成一次
ints
表,向其中添加索引,然后使用上述技术来执行所有插入就在数据库里面。其他数据库有自己的方式(例如PostgreSQL的generate_series
)不需要显式的ints
表就能完成这类事情,但SQLite(有意)受到限制。SQL 通常是基于集合的,因此循环并不自然。自然的做法是通过描述您的需求来构建适当的集合。 OTOH,有时不自然的行为是必要且明智的。
我不知道这对您的应用程序是否有意义,我只是想演示一下如何完成它。如果这种方法对您的情况没有意义,那么您可以在数据库外部生成一堆 INSERT 语句。
You can do this sort of thing in straight SQL if you have an extra table that holds all the integers that you need.
Suppose your
StartRange
andEndRange
range between one and ten and you have a table like this:This table simply contains all the possible integers that you need (i.e. one through ten).
Then if you also have this:
You can do your INSERTs into
target
with a join:The result is this:
Of course your real
t
would have more rows so you'd want a WHERE clause to limit which row oft
you look at.Similar things are often done with dates (look up "calendar tables").
So if your ranges are small (for some definition of small) then generate your
ints
table once, add an index to it, and use the above technique to do all the INSERTs right inside the database. Other databases have their own ways (such as PostgreSQL'sgenerate_series
) to do this sort of thing without need an explicitints
table but SQLite is (intentionally) limited.SQL is generally set-based so loops aren't natural. What is natural is building the appropriate sets by describing what you need. OTOH, sometimes unnatural acts are necessary and sensible.
I don't know if this makes sense for your application, I just thought I'd demonstrate how it can be done. If this approach doesn't make sense in your case then you can generate a bunch of INSERT statements outside the database.
SQL 中不需要循环,因为没有 WHERE 子句
该命令将对表中的每一行执行。
只需使用:(
并提交更改),因此
Endrange
列中的每个数字都会比Startrange
列中的数字大 2。There is no need for loops in SQL, because without the WHERE clause
the command will be executed for every row in the table.
Use simply:
(and commit the changes), so each number in the
Endrange
column will be greater by 2 compared to the number in theStartrange
column.可以将数字序列生成为虚拟表,并可以选择开始、停止和步骤。
此选择可能会插入到您的表中:
祝你好运,快乐的 sqlite
a sequence of numbers can be generated as a virtual table, with start, stop and step you choose.
this select may be inserted in your table as:
good luck and happy sqlite