数据库
首页 > 数据库> > mysql – 播种SQLite RANDOM()

mysql – 播种SQLite RANDOM()

作者:互联网

SQLite是否支持播放RANDOM()函数,就像MySQL对RAND()一样?

$query = "SELECT * FROM table ORDER BY RAND(" . date('Ymd') . ") LIMIT 1;";

从MySQL手册关于兰德(N):

If a constant integer argument N is
specified, it is used as the seed
value, which produces a repeatable
sequence of column values. In the
following example, note that the
sequences of values produced by
RAND(3) is the same both places where
it occurs.

如果没有,有没有办法只使用一个查询存档相同的效果?

解决方法:

如果你需要一个伪随机顺序,你可以做这样的事情(PHP):

$seed = md5(mt_rand());
$prng = ('0.' . str_replace(array('0', 'a', 'b', 'c', 'd', 'e', 'f'), array('7', '3', '1', '5', '9', '8', '4'), $seed )) * 1;
$query = 'SELECT id, name FROM table ORDER BY (substr(id * ' . $prng . ', length(id) + 2)';

另外,您可以将$seed设置为预定义值,并始终获得相同的结果.

我从同事http://steamcooker.blogspot.com/那里学到了这个技巧

标签:sql,mysql,sqlite,random,seeding
来源: https://codeday.me/bug/20191002/1843456.html