具有多个联接的重复行

时间:2019-04-02 04:23:15

标签: php mysql laravel eloquent

我正在使用此代码在内容表上联接两个表,然后按用户分组 当我仅加入分析表时,结果也是正确的。 但两个表的总和计数均导致错误的结果

->where('users.type_id', 2)
->leftJoin('contents', 'users.id', '=', 'contents.instructor_id')
->leftJoin('analytics', 'contents.id', '=', 'analytics.content_id')
->leftJoin('reactions', 'contents.id', '=', 'reactions.content_id')

->selectRaw('
    users.id as id,
    concat(users.name, "-", users.last_name) as name,
    sum(analytics.total_watched) as sum_watched,
    count(analytics.id) as times_watched,
    sum(analytics.completed=1) as completed_video,

    sum(reactions.reaction_type="love") as love,
    sum(reactions.reaction_type="happy") as happy,
    sum(reactions.reaction_type="thinking") as thinking,
    sum(reactions.reaction_type="sad") as sad,
    sum(reactions.reaction_type="angry") as angry                    
')
->groupBy('users.id')
->limit(10)
->get();

0 个答案:

没有答案