2017-07-26 67 views
0

我有產品表nameno_of_sell列.. 我想根據no_of_sell列從產品表中獲得最高5的銷售產品。 如何將它與查詢生成器一起使用?如何從列中獲得最高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 
+1

您可以使用排序依據[https://laravel.com/docs/5.4/queries#ordering-grouping-limit (https://laravel.com/docs/5.4/queries#ordering-grouping-limit-and-offset) –

回答

0
$best_sell = DB::table('products') 
     ->select('no_of_sell') 
     ->where('products.status','=','1')      
     ->orderBy('no_of_sell', 'desc') 
     ->paginate(4); 
+0

我用了desc,但問題是它沒有顯示正確的。假設no_of_sell的值爲7 9 10 12 15 ..如果我想要得到4個數據,它應該返回9 10 12 15,但它返回7 9 12 15 – User57

0
$best_sell = DB::table('products') 
     ->select() // your selection criteria 
     ->where('products.status','=','1') 
     ->take(5)->get(); 
+0

我想查找最高的5個數值.. – User57

+0

它需要最高5值。 –