I am using Laravel's Eloquent ORM to query a database for infomation, but for some reason the resulting query isn't returning all of the rows.
Eloquent Query:
\Permit::selectRaw('CONCAT(lk_departments.dept_id, ".") AS dept_id, '.
'lk_departments.dept_name, '.
'COUNT(DISTINCT permits.permit_number) AS total')
->leftJoin('lk_departments', 'permits.dept_id', '=', 'lk_departments.dept_id')
->where('permits.permit_number', '!=', 'N/A')
->whereBetween('permits.valid_date', [$query_info['start_date'], $query_info['end_date']])
->groupBy('lk_departments.dept_name')
->orderBy('lk_departments.dept_name', 'ASC')
->get()
Resulting Query:
SELECT
CONCAT(lk_departments.dept_id, ".") AS dept_id, lk_departments.dept_name,
COUNT(DISTINCT permits.permit_number) AS total
FROM permits
LEFT JOIN lk_departments ON permits.dept_id = lk_departments.dept_id
WHERE permits.valid_date BETWEEN '01/01/2000' AND '12/12/2100'
GROUP BY lk_departments.dept_name
ORDER BY lk_departments.dept_name ASC
Returned Data:
dept_id dept_name total
+--------------------------------------------------------+
| NULL NULL 0 |
| 1. Academic Achievement-(Special Services) 11 |
| 3. Academic Clubs 1 |
| 4. Academic Custodial & Grounds Services 1 |
| 5. Accounting 2 |
| 6. Admissions 356 |
| 7. Advanced Programs Dept (Education Dept) 8 |
| 9. Aerospace Studies - Air Force ROTC 1 |
| 11. American Federation of Teachers 0 |
| 13. Anthropology 1 |
| 14. Art Dept. 4 |
| 17. Athletic Dept. 11 |
| 21. Biology Dept. 2 |
| |
| ... ... ... |
+--------------------------------------------------------+
I referenced this question to modify total: Count distinct values
but I still receive one NULL row with a total of 0 and missing records. I'm not very good with MySQL, so forgive me if I am missing something. Ideally, I would like a count of permits from each department for a given time frame.
NOTE:dept_id = auto incremented primary keypermits = table containing parking permit information that is linked to a department
dept_id IS NOT NULLnot omit that unwanted row? - tadmandept_id IS NOT NULLreturns an empty set. - user3562712.to the number, instead save that for when it's shown in whatever view it's ending up in. - tadman