【问题标题】:Laravel lookup avg result in other tableLaravel 在其他表中查找平均结果
【发布时间】:2020-03-20 01:40:53
【问题描述】:

我在 Laravel 中有这个查询:

DB::table('score')
    ->select('score.score_nl', DB::raw('count(*) as total'), DB::raw('round(avg(rating_results.rating)) as final_rating'))
    ->join('rating', 'rating.score_id', '=', 'score.id')
    ->join('rating_results', 'rating.rating_result_id', '=', 'rating_results.id')
    ->groupBy('score_nl')
    ->get();

结果:

[{"score_nl":"emphatisch","total":1,"final_rating":"1"},{"score_nl":"huilen","total":2,"final_rating":"3"},{"score_nl":"knuffelig","total":1,"final_rating":"1"},{"score_nl":"zindelijkheid","total":2,"final_rating":"3"}]

我想在这个表中有一个名为rating_results(见图片)的表 查找final_rating 并获取关联的result_en

我怎么能在 laravel 中做到这一点?

有任何问题请告诉我!

--编辑--

这个我试过了;

$q = Result::select('result_nl')
    ->whereColumn('rating_results.rating', 'final_rating')
    ->whereColumn('rating_results.score_id', 'score_id')
    ->getQuery();

DB::table('score')
    ->select('score.score_nl', DB::raw('count(*) as total'), DB::raw('round(avg(rating_results.rating)) as final_rating'))
    ->join('rating', 'rating.score_id', '=', 'score.id')
    ->join('rating_results', 'rating.rating_result_id', '=', 'rating_results.id')
    ->selectSub($q, 'result_nl')
    ->groupBy('score_nl')
    ->get();

然后我得到这个错误:

SQLSTATE[42S22]: Column not found: 1247 Reference 'final_rating' not supported (reference to group function) (SQL: select `score`.`score_nl`, count(*) as total, round(avg(rating_results.rating)) as final_rating, (select `result_nl` from `rating_results` where `rating_results`.`rating` = `final_rating` and `rating_results`.`score_id` = `score_id`) as `result_nl` from `score` inner join `rating` on `rating`.`score_id` = `score`.`id` inner join `rating_results` on `rating`.`rating_result_id` = `rating_results`.`id` group by `score_nl`)

看来我需要使用joinSub

【问题讨论】:

    标签: mysql sql laravel laravel-query-builder


    【解决方案1】:

    你很亲密。你可以在这里使用joinSub

    从您的尝试中我可以看出,您还需要通过score_id 加入吗?如果是这样,这是解决方案:

    // Your initial query
    $query = DB::table('score')
        ->selectRaw('
            score.score_id, 
            score.score_nl, 
            count(*) as total, 
            round(avg(rating_results.rating)) as final_rating
        ')
        ->join('rating', 'rating.score_id', '=', 'score.id')
        ->join('rating_results', 'rating.rating_result_id', '=', 'rating_results.id')
        ->groupBy('score_nl');
    
    // Now use joinSub (just like simple joins above)
    $result = DB::table('rating_results')
        ->joinSub($query, 'subtable', function($join) {
            $join->on('subtable.final_rating', '=', 'rating_results.rating')
                 ->on('subtable.score_id', '=', 'rating_results.score_id');
        })
        ->selectRaw('subtable.*, rating_results.result_en')
        ->get();
    

    但我个人会在这里使用原始 SQL。当查询变得过于繁琐时,最好避免使用 ORM,这样您就不会局限于它的功能:

    $result = DB::select('
        select subtable.*, rating_results.result_en
        from rating_results
        join (
            select
                score.score_nl, 
                count(*) as total, 
                round(avg(rating_results.rating)) as final_rating 
            from score
            join rating on rating.score_id = score.id
            join rating_results on rating.rating_result_id = rating_results.id 
            group by score_nl
        ) as subtable on subtable.final_rating = rating_results.rating
    ');
    

    【讨论】:

    • 我喜欢您的陈述:“当查询变得过于繁琐时,最好避免使用 ORM,这样您就不会局限于它的功能:”
    • 在您的原始查询中,您不会通过 score_id 加入。另请注意,如果您选择 score_id(在子查询中),它也应该列在 group by 子句中。
    猜你喜欢
    • 2017-07-07
    • 2017-08-13
    • 1970-01-01
    • 1970-01-01
    • 2013-11-09
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    相关资源
    最近更新 更多