Skip to content
Advertisement

How to do this join query in Laravel way?

select tb_addquestion.UserId, COUNT(*) AS count from `tb_addquestion` 
 left join `tb_user` on `tb_user`.`UserId` = `tb_addquestion`.`UserId`
 GROUP BY(tb_addquestion.UserId) having count > 0 AND count < 15

Advertisement

Answer

DB::table('tb_addquestion')
    ->leftJoin('tb_user', 'tb_user.UserId', 'tb_addquestion.UserId')
    ->groupBy('tb_addquestion.UserId')
    ->having('count', '>', 0)
    ->having('count', '<', 15)
    ->select('tb_addquestion.UserId', DB::raw('COUNT(*) AS count'));
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement