Warm tip: This article is reproduced from serverfault.com, please click

Count nested select using join and by month at laravel

发布于 2020-11-28 09:28:34

Hi how to convert this query to laravel ORM, or Eloquent? And the oy field now returns number of month, i want to get name of month.

SELECT c.name as company, i.title_uz as internet, EXTRACT(MONTH FROM  ci.created_at) as oy, COUNT(*) AS miqdor
FROM count_internets AS ci
JOIN companies as c ON  ci.company_id = c.id
JOIN internets as i ON  ci.u_id = i.id
GROUP BY company, internet, oy
Questioner
Hayrulla Melibayev
Viewed
11
OMR 2020-11-28 17:41:23

for month name, you can use MySQL MONTHNAME() Function

  DB::table("count_internets")->join("companies","companies.id",'=','count_internets.company_id')
           ->join("internets",'internets.id','=','count_internets..u_id')
           ->select(["count_internets.name as company",'internets.title_uz as internet',DB::raw("MONTHNAME(companies.created_at) as oy")
               ,DB::raw("COUNT(*) AS miqdor")])
           ->groupBy(['company','internet','oy'])->get();