I have the following tables:
CREATE TABLE `accommodations` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
)
CREATE TABLE `accommodations_exclude` (
`id_accommodation` int(11) unsigned NOT NULL,
`id_course` int(11) NOT NULL,
UNIQUE KEY `id_course` (`id_course`,`id_accommodation`)
)
In the accommodations table there are 4 records, in the accommodations_exclude there are many more. Now I would like to have a query that always give me all records from the accommodations table, and joined as extra field to see if the accommodation also exists in the accommodations_exclude table.
For example; in the accommodations_exclude there is one row with id_accommodation = 2, id_course = 16.
I want to have a resultset that shows me the following:
accommodation.id, accommodation.name, accommodation_exclude.id_accommodation, accommodation_exclude.id_course
1,'acco 1',null,null
2,'acco 2',2,16
3,'acco 3',null,null
4,'acco 4',null,null
The query I have right now is this one:
SELECT *
FROM accommodations a
LEFT JOIN accommodations_exclude ae ON a.id = ae.id_accommodation
WHERE ae.id_course = 16
but this gives me only the resultset
2,'acco 2',2,16
and not the accommodations that should have null values
any idea on what i do wrong here ?
Move the
ae.id_course = 16clause fromWHEREto yourLEFT JOINYou should think of
WHEREas a filter on the final resultset, anything to do with linking tables should be conditions in yourJOINs.Your original
WHERE ae.id_course = 16was filtering out the NULL ae.id_course rows from the resultset.