- SELECT * FROM tableName ORDER BY RAND() LIMIT n 数据量小的话还可以, 数据量大起来了, 就影响性能了。
$rubbish = (new \yii\db\Query()) ->select('r.name as rubbish,r.id as rid, c.id, c.name, c.code, c.inc, c.des,c.req') ->from('recycle_rubbishkeytocategory AS r') ->leftJoin('recycle_rubbishcategory AS c','c.id = r.category_id') ->where([]) ->orderBy('id', rand(1,1000)) ->limit(10) ->All();
- 根据表中的ID, 假设表中有1000条数据,那么先生成1-1000的随机数,比如放进数组中,然后遍历,然后where id = randnum,循环查询n次。或者使用 where in 查询。想不到别的办法了。
- yii随机查询数据-》点击这里