2013-08-04 77 views
0

我需要下面的SQL師訂單轉換爲佞Laravel 3 - 利用兩個場的

select * from medias order by likes/views DESC, views ASC 

我需要使用分頁的結果,這就是爲什麼我喜歡侃侃而談。

我的一些其他的SQL查詢的是

$media_list = Media::order_by('likes', 'desc')->paginate($per_page); 

我試圖

$media_list = Media::order_by('likes/views', 'desc')->paginate($per_page); 

但它給錯誤

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'likes/views' in 'order clause' 
SQL: SELECT * FROM `medias` ORDER BY `likes/views` DESC LIMIT 20 OFFSET 0 

任何人都知道如何解決這一問題?

回答

1

嘗試,代替/

$media_list = Media::order_by('likes,views', 'desc')->paginate($per_page); 

$media_list = Media::order_by('likes`,`views', 'desc')->paginate($per_page); 

而且也這是laravel做的標準方式

$media_list = Media::order_by('likes', 'desc')->orderBy('views', 'desc')->paginate($per_page); 

$media_list = DB::table('medias') 
    ->select(DB::raw('(likes/views) AS resultant')) 
    ->order_by('resultant', 'desc')->orderBy('views', 'desc') 
    ->get(); 
+0

看到我更新的答案 –

+0

TY更新的代碼
$ media_list = DB :: table('medias ') - > select(DB :: raw('(likes/views)AS')) - > order_by('results','desc') - > order_by('views','desc') - > get );
失敗,錯誤
SQLSTATE [42S22]:未找到列:1054'字段列表'中的未知列'(likes/views)'
SQL:SELECT'(likes/views)'AS''合成結果'從'medias' ORDER BY結果「DESC,」views「DESC – BizHat