如何使用每行的自动递增值将查询插入到 SQLite 中 [英] How to insert a query into SQLite with an autoincrementing value for each row
本文介绍了如何使用每行的自动递增值将查询插入到 SQLite 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我将以下查询集插入到 SQLite 的新表中:
Suppose I am inserting the following queryset into a new table in SQLite:
CREATE TABLE queryset_cache AS
SELECT ROW_NUMBER() over () AS rowid, * FROM mytable ORDER BY product;
是否可以:
- 将
rowid
设置为来自插入的 sqlite 中的自动递增 PK,或者; - 排除 rowid 并让 SQLite 为每个插入的记录自动添加一个自动递增的主键.
- Set the
rowid
as auto-incrementing PK in sqlite from the insert, or; - Exclude the rowid and have SQLite auto-add in an autoincrementing primary key for each inserted record.
这将如何完成?
目前,没有那个,当我进行插入时,rowid 不会被索引.
Currently, without that when I do the insert, the rowid is not indexed.
推荐答案
rowid
已经存在.你可以这样做:
rowid
is already there. You can just do:
CREATE TABLE queryset_cache AS
SELECT t.*
FROM mytable t
ORDER BY product;
如果你这样做,你会看到它:
You will see it if you do:
SELECT rowid, t.*
FROM queryset_cache;
这里是db<>fiddle
Here is a db<>fiddle
这篇关于如何使用每行的自动递增值将查询插入到 SQLite 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文