2 回答
TA贡献1796条经验 获得超10个赞
你把它和 PHP 混在一起
$sql = 'SELECT * FROM `identity_explorer_demographics`';
//Perform query and parse result, E.G $sql = db::query($sql);
function getRandomRow($sql) {
$random_row = count(0, count($sql));
foreach($sql as $row => $result) {
if ($row == $random_row) {
return $row;
}
}
}
TA贡献1829条经验 获得超7个赞
$sql = "SELECT * FROM identity_explorer_demographics ORDER BY RAND() LIMIT 1";
将行更改为
$ randomv = rand (min, max);
$sql = "SELECT * FROM identity_explorer_demographics ORDER BY $randomv LIMIT 1";
这是我在另一种情况下的实现,我必须从可用的随机 id 中进行选择 - Yii 2 Framework
$prodcutids= \app\models\TblProduct::find()->all();
$targetproduct= ArrayHelper::map($prodcutids, 'id','id');
$productdetails= \app\models\TblProduct::findOne(['id'=>array_rand($targetproduct)]);
$productseriesname= \app\models\TblSeries::findOne(['id'=>$productdetails['Serie']]);
或者,如果您有一个 id 列
$sql = "SELECT * FROM identity_explorer_demographics WHERE id=$randomv LIMIT 1";
rand(min,max) min 指定将返回的最小值。max 指定要返回的最大值。
- 2 回答
- 0 关注
- 163 浏览
添加回答
举报