SQLite – ORDER BY RAND()

前端之家收集整理的这篇文章主要介绍了SQLite – ORDER BY RAND()前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
MysqL中我可以使用RAND()函数,在sqlite 3中有什么替代方法吗?
使用 random()
SELECT 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内部行标识的别名。

原文链接:https://www.f2er.com/sqlite/198286.html

猜你在找的Sqlite相关文章