我有一个测试sqlite表:
CREATE TABLE test ( id INTEGER PRIMARY KEY NOT NULL,x REAL NOT NULL DEFAULT (RANDOM()),y REAL NOT NULL DEFAULT (RANDOM()),z REAL NOT NULL DEFAULT (RANDOM()) )
我想创建超过数千行的随机数据,我怎样才能使用SQL查询?我不能使用sql FOR循环,因为sqlite不支持循环,我正在考虑从表中选择所有数据然后INSERT为每一行,因此每次执行查询时行数加倍.我认为这需要嵌套查询,我不明白.
为了hacky,这将一次插入多达2048个随机行,通过设置限制来决定多少行;
原文链接:https://www.f2er.com/sqlite/197580.htmlINSERT INTO TEST (x,y,z) SELECT RANDOM(),RANDOM(),RANDOM() FROM (SELECT * FROM ( (SELECT 0 UNION ALL SELECT 1) t2,(SELECT 0 UNION ALL SELECT 1) t4,(SELECT 0 UNION ALL SELECT 1) t8,(SELECT 0 UNION ALL SELECT 1) t16,(SELECT 0 UNION ALL SELECT 1) t32,(SELECT 0 UNION ALL SELECT 1) t64,(SELECT 0 UNION ALL SELECT 1) t128,(SELECT 0 UNION ALL SELECT 1) t256,(SELECT 0 UNION ALL SELECT 1) t512,(SELECT 0 UNION ALL SELECT 1) t1024,(SELECT 0 UNION ALL SELECT 1) t2048 ) ) LIMIT 246;