2015-02-09 56 views
0

請告訴我該怎麼辦這一關正確的查詢使用原始表達式Laravel 4

select @a:=POW(max(count_votes), 1/10) from users; 

select id,name,raiting, count_votes, ((LOG(@a,count_votes))+raiting)/2 as actual_raiting from users order by actual_raiting desc ; 

試過這樣

$rating = DB::statement('select @a:=POW(max(count_votes), 1/10) from users; 

select id,name,raiting, count_votes, ((LOG(@a,count_votes))+raiting)/2 as actual_raiting from users order by actual_raiting desc; '); 

return $rating; 

$rating = DB::table('users') 
        ->select(DB::raw('*, @a:=POW(max(count_votes), 1/10) ')) 
        ->select(DB::raw('*,raiting, count_votes, ((LOG(@a,count_votes))+raiting)/2 as actual_raiting')) 
        ->orderBy("actual_raiting") 
        ->get(); 

    return $rating; 

我究竟做錯了什麼?

PS:我的英語水平

回答

0

嘗試把它們都放在一個選擇:

$rating = DB::table('users') 
      ->select(DB::raw('*, raiting, count_votes, ((LOG(POW(max(count_votes), 1/10),count_votes))+raiting)/2 as actual_raiting')) 
      ->orderBy("actual_raiting") 
      ->get(); 
+0

謝謝!它幫助我 – Rai 2015-02-09 08:27:28

+0

不客氣。如果這有效,請[標記爲已接受](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)。如果不是,請告訴我應該更改/編輯或添加自己的答案:) – lukasgeiter 2015-02-09 08:30:00

+0

如何僅獲取字段'actual_rating'? 他告訴我要返回所有數據 – Rai 2015-02-09 08:36:12

0

/不好意思試試這個:

$rating1 = DB::select(DB::raw('select @a:=POW(max(count_votes), 1/10) from users')); 

$rating2 = DB::select(DB::raw('select id,name,raiting, count_votes, ((LOG(@a,count_votes))+raiting)/2 as actual_raiting from users order by actual_raiting desc; ')); 

echo "<pre>"; 
print_r($rating1); 
print_r($rating2); 
echo "</pre>"; 
exit; 

看,如果沒有什麼幫助。

+0

SQLSTATE [42000]:語法錯誤或訪問衝突:1064您的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在第3行(選擇id,名稱,raiting,count_votes,((LOG(@ a,count_votes))+ raiting)/ 2附近使用正確的語法:從用戶選擇@a:= POW(max(count_votes),1/10);選擇id,name,raiting,count_votes,((LOG(@ a,count_votes))+ raiting)/ 2作爲actual_raiting from users order by actual_raiting desc;) – Rai 2015-02-09 08:13:32

+0

因此,這意味着您的SQL查詢不正確,請嘗試在phpmyAdmin中運行它,糾正並重試。 – 2015-02-09 08:14:56

+0

http://i58.fastpic.ru/big/2015/0209/4b/8cc0eaf78c05cf52a8f5864ff052a84b.png – Rai 2015-02-09 08:15:14