tp从数据库中随机查n条数据
发布时间:2022-09-13, 10:04:03 分类:PHP | 编辑 off 网址 | 辅助
正文 473字数 343,004阅读
$num = 10;//几条数据
$count = Db::name( 'topic' )->count();
$rand = mt_rand( 0, $count-1 );
$limit = $rand.','.$count;
$list = Db::name( 'topic' )->limit( $limit )->select();
shuffle( $list );
$new_ary = array_slice( $list, 0, $num );
Run code
Cut to clipboard
$new_ary = Db::name('topic')->orderRaw('rand()')->limit(10)->select();
Run code
Cut to clipboard
(支付宝)给作者钱财以资鼓励 (微信)→
有过 1 条评论 »
use think\Db; $in = ['张三', '李四', '王五']; $order= 'field(name, '.$in.')'; return User::whereIn('name',$in)->order(Db::raw($order))->select();