使用
random():
原文链接:https://www.f2er.com/sqlite/198286.htmlSELECT foo FROM bar WHERE id >= (abs(random()) % (SELECT max(id) FROM bar)) LIMIT 1;
编辑(通过QOP):由于SQLite Autoincremented列上的文档说明:
The normal ROWID selection algorithm described above will generate
monotonically increasing unique ROWIDs as long as you never use the
maximum ROWID value and you never delete the entry in the table with
the largest ROWID. If you ever delete rows,then ROWIDs from
prevIoUsly deleted rows might be reused when creating new rows.
以上只是真的,如果你没有一个INTEGER PRIMARY KEY AUTOINCREMENT列(它仍然可以正常工作与INTEGER PRIMARY KEY列)。无论如何,这应该更加便携/可靠:
SELECT foo FROM bar WHERE _ROWID_ >= (abs(random()) % (SELECT max(_ROWID_) FROM bar)) LIMIT 1;
ROWID,_ROWID_和OID都是sqlite内部行标识的别名。