tp從數據庫中隨機查n條數據

方法一:

            $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 );

方法二:

            $new_ary = Db::name('topic')->orderRaw('rand()')->limit(10)->select();

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章