I have two tables, a vehicle
table with columns:
id
stock
year
make
model
and an images
table with columns:
id
vehicle_id
name
caption
default tinyint(1)
I am trying to list the vehicle's information, its default image, and a total count of images the vehicle has. Currently I am using the following SELECT
statement:
SELECT vehicle.id, vehicle.stock, vehicle.year,
vehicle.make, vehicle.model, images.name,
COUNT(images.id)
FROM vehicle
LEFT JOIN images
ON vehicle.id = images.vehicle_id
I initially was using:
ON vehicle.id = images.vehicle_id AND images.default = 1
but then the images count would only be 1 or 0 depending if there was a default image in the database. I have tried using UNION
and other SELECT
statements but I am still unable to get a proper result. Do I need to use two SELECT
statements or is there another way to handle it with JOIN
or UNION
?