I am attempting to join two tables to my query and count the number of matching rows from each of the joins. It seems to come back with the wrong result though. It seems to be doubling the results. Here is the query
SELECT *, COUNT(car_id) AS numCars, COUNT(van_id) AS numVans
FROM company_branch
LEFT JOIN car ON car_branchid = company_branch_id
LEFT JOIN van ON van_branchid = company_branch_id
WHERE company_branch_userid = 1
GROUP BY company_branch_id,
ORDER BY company_branch_active DESC, company_branch_name ASC
I’m assuming that
car_idis the primary key on thecartable andvan_idis the primary key on thevantable. In that case, useDISTINCTto get the counts in each group individually. Try this: