使用原始表达式Laravel 4

时间:2015-02-09 08:05:55

标签: sql 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:/抱歉我的英文

2 个答案:

答案 0 :(得分: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;

看,如果有帮助。

答案 1 :(得分: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();