laravel - Eloquent ORM resulting MySQL select not returning all rows -
i using laravel's eloquent orm query database infomation, reason resulting query isn't returning of rows.
eloquent query:
\permit::selectraw('concat(lk_departments.dept_id, ".") dept_id, '. 'lk_departments.dept_name, '. 'count(distinct permits.permit_number) 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, ".") dept_id, lk_departments.dept_name, count(distinct permits.permit_number) total permits left join lk_departments on permits.dept_id = lk_departments.dept_id permits.valid_date between '01/01/2000' , '12/12/2100' group lk_departments.dept_name order 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 | | | | ... ... ... | +--------------------------------------------------------+
referenced question modify total
: count distinct values
but still receive 1 null
row total
of 0
, missing records. i'm not mysql, forgive me if missing something. ideally, count of permits each department given time frame.
note:
dept_id
= auto incremented primary key
permits
= table containing parking permit information linked department
i figured out problem. when using permits
master table in join, neglected realize records dept_id
wouldn't show if there no corresponding records permits.dept_id
matched lk_departments.dept_id
(if nobody had registered permit department, in other words). also, reason, had change query include records permits.permit_number null
.
resulting eloquent query:
lkdepartment::selectraw('lk_departments.*, count(permits.permit_number) total') ->leftjoin('permits', 'lk_departments.dept_id', '=', 'permits.dept_id') ->wherenull('permits.permit_number') ->orwhereraw("(permits.permit_number != 'n/a' , permits.valid_date between '$query_info[start_date]' , '$query_info[end_date]')") ->groupby('lk_departments.dept_id') ->orderby('lk_departments.dept_id', 'asc') ->get()
resulting query:
select lk_departments.* , count( permits.permit_number ) total lk_departments left join permits on lk_departments.dept_id = permits.dept_id permits.permit_number null or ( permits.permit_number != 'n/a' , permits.valid_date between '01/01/2000' , '12/12/2100' ) group lk_departments.dept_id order lk_departments.dept_id asc
returned data:
dept_id dept_name total +--------------------------------------------------------+ | null null 0 | | 1 academic achievement-(special services) 11 | | 2 academic advising center 0 | | 3 academic clubs 1 | | 4 academic custodial & grounds services 1 | | 5 accounting 2 | | 6 admissions 356 | | 7 advanced programs dept (education dept) 8 | | 8 advisory council 0 | | 9 aerospace studies - air force rotc 1 | | 10 alternative school 0 | | 11 american federation of teachers 0 | | 12 anthropology 0 | | | | ... ... ... | +--------------------------------------------------------+
Comments
Post a Comment