首页 > 解决方案 > 具有多个连接的重复行

问题描述

我正在使用此代码在内容表上加入两个表,然后在我仅加入分析表时按用户分组,结果也是正确的

->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();

标签: phpmysqllaraveleloquent

解决方案


推荐阅读