簡體   English   中英

如何從列中獲取最高的5個值?

[英]how to get highest 5 values from column?

我有帶nameno_of_sell列的產品表。.我想從產品表中獲得基於no_of_sell列的最高5個售出產品。 如何與查詢生成器一起使用?

  $propular_products =  DB::table('users')
            ->join('products','products.auth_id','users.id')
            ->select('products.*')   
            ->orderBy('products.no_of_sell', 'desc')              
            ->where('products.status','=','1') 
            ->paginate(5);

假設products表:

name no_of_sell
 x     6
 y     9
 z     10
 t     23
 u     3
 h     11
 r      5

我想找到

products list of 5 max no_of_sell ie, x y z t h 

因此,如果我在列中正確理解它,則no_of_sell是一個整數。 我應該這樣寫:

$best_sell = DB::table('products')
             ->orderBy('no_of_sell', 'desc')
             ->limit(5)
             ->where('products.status','=','1') 
             ->paginate(4)
             ->get(); 

https://laravel.com/docs/5.4/queries#retrieving-results https://laravel.com/docs/5.4/queries#ordering-grouping-limit-and-offset

$best_sell = DB::table('products')
         ->select('no_of_sell')   
         ->where('products.status','=','1')                       
         ->orderBy('no_of_sell', 'desc')
         ->paginate(4);
$best_sell = DB::table('products')
        ->select() // your selection criteria 
        ->where('products.status','=','1') 
        ->take(5)->get(); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM