播种SQLite RANDOM() [英] Seeding SQLite RANDOM()

查看:87
本文介绍了播种SQLite RANDOM()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SQLite是否支持为 RANDOM() 函数设置种子MySQL对 RAND() ?

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

关于RAND(N)的MySQL手册:

如果常量整数参数N为 指定,用作种子 值,产生可重复的 列值的顺序.在里面 在以下示例中,请注意 由...产生的值序列 RAND(3)在两个地方都相同 它发生了.

如果没有,是否有任何方法可以仅使用一个查询来归档相同的效果?

解决方案

如果您需要伪随机顺序,则可以执行以下操作(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/ >

Does SQLite support seeding the RANDOM() function the same way MySQL does with RAND()?

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

From the MySQL Manual about RAND(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.

If not, is there any way to archive the same effect using only one query?

解决方案

If you need a pseudo-random order, you can do something like this (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)';

Plus, you can set $seed to the predefined value and always get same results.

I've learned this trick from my colleague http://steamcooker.blogspot.com/

这篇关于播种SQLite RANDOM()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆