Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT DISTINCT values after a JOIN

I have 3 tables:

Vehicle: vehicle_id, vehicle_type
1, motorcycle
2, car
3, van

Owners: person_id, vehicle_id, date_bought
1, 1, 2009
1, 2, 2008
2, 3, 2009
2, 1, 2005

I want to display a list of all vehicle names. If the person_id = 1, date_bought should also be returned.

So I thought I would start with this:

SELECT * FROM vehicles
LEFT JOIN Owners USING (vehicle_id)

which returns this:

1, 1, motorcycle, 2009
1, 2, car, 2008
2, 3, van, 2009
2, 1, motorcycle, 2005

However, I now cannot narrow this down to the needed result. If I use DISTINCT(car_id), there is no change as I am already choosing distinct car ids before the JOIN; they are only non-distinct after the join. If I use WHERE person_id = 1, I remove the last 2 rows and all reference to the van is gone. If I use GROUP BY car_id, the first and last rows are combined but the date_bought for the motorcycle is chosen arbitrarily. What I want is this:

1, 1, motorcycle, 2009
1, 2, car, 2008
 , 3, van, 

I need to require a distinct car id but this happens before the JOIN and so has no effect at all. How can I get the uniqueness with the JOIN?

like image 282
Rupert Madden-Abbott Avatar asked Dec 22 '09 05:12

Rupert Madden-Abbott


1 Answers

You need to include the restriction on person id in your join and use an outer join. Outer joins are used when you want to return information even if there are no records in the table you're joining to. Try

SELECT person_id, vehicles.* 
FROM vehicles 
LEFT OUTER JOIN Owners on vehicles.vehicle_id = owners.vehicle_id 
and person_id = 1
like image 190
Robert Christie Avatar answered Oct 07 '22 10:10

Robert Christie