Skip to content

Instantly share code, notes, and snippets.

@forecho
Created November 8, 2015 05:28
Show Gist options
  • Select an option

  • Save forecho/cc1139bcbba5de6e5df1 to your computer and use it in GitHub Desktop.

Select an option

Save forecho/cc1139bcbba5de6e5df1 to your computer and use it in GitHub Desktop.
百万数据库某个表「伪」随机显示 N 条数据思路
/**
* 随机
* @param int $rows 数据池
* @return mixed
*/
protected function random($rows = 100)
{
$count = User::find()->select(['itemid'])->where([])->count();
$random = mt_rand($rows, $count - $rows);
$model = User::find()->where(['>=', 'id', $random])->joinWith(['type'])->limit($rows)->all();
shuffle($model); // 洗牌
return $model;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment