I m not that good in SQL and I ve come across a problem I don t know how to solve. I ve read and re-read parts of a book about SQL (O Reilly s Learning SQL) which I hoped would contain the information I needed but I haven t found it.
My problem is the following. I ll use a simplified example to make it easier to discuss.
I ve got three tables, car, rim and the combination of the two: carRim.
car
carId
description
rim
rimId
description
carRim
carRimId
carId
rimId
price
In the table carRim I have an extra attribute of price, because the price of a rim is potentially different for every type of car. A constraint I have is that every type of rim should only be coupled once to every type of car. So all combinations of car-rim should be unique.
If I want to add a rim to a car I need a list of rims that are not yet coupled to that car. For this I think I need the rim table and the carRim table respectively for the total list of rims and the list of carRims that are already coupled to the car I want to add a rim to.
I ve written the (simple) query to make the list of rims that are coupled to a specific car, in the following example the car with carId 9.
SELECT
*
FROM
rims
INNER JOIN
carRims
ON
carRims.rimId = rim.rimId
WHERE
carRims.carId = 9
But now I need the list of rims that are not yet coupled to a specific car. The problem is that if I do a LEFT OUTER JOIN the list I get is "tainted" with couplings of rims to other cars, so the filter condition of "WHERE carRims.carId IS NULL" does not work.
SELECT
*
FROM
rims
LEFT OUTER JOIN
carRims
ON
carRims.rimId = rim.rimId
WHERE
carRims.carId IS NULL
Another challenge is that I can t use any syntax new to MySQL 5, like subqueries, because my client is using MySQL 4 and can t upgrade at this time.
Can a query for this problem be written in MySQL 4, I suspect it can.
Thanks!