I'd like some help with an left join statement thats not doing what i, probably incorrectly, think it should do.
there are two tables:
cd:
CREATE TABLE `cd` ( `itemID` int(11) NOT NULL AUTO_INCREMENT, `title` text NOT NULL, `artist` text NOT NULL, `genre` text NOT NULL, `tracks` int(11) NOT NULL, PRIMARY KEY (`itemID`) ) loans
CREATE TABLE `loans` ( `itemID` int(11) NOT NULL, `itemType` varchar(20) NOT NULL, `userID` int(11) NOT NULL, `dueDate` date NOT NULL, PRIMARY KEY (`itemID`,`itemType`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; and i want to select all cd's thats not in loans using a left join and then an where dueDate is null
select t.itemID, t.artist as first, t. title as second, (select AVG(rating) from ac9039.ratings where itemType = 'cd' and itemId = t.itemID) as `rating avarage`, (select COUNT(rating) from ac9039.ratings where itemType = 'cd' and itemId = t.itemID) as `number of ratings` from cd t left join loans l on t.itemID = l.itemID where l.itemType = 'cd' and l.dueDate is null; this one however returns an empty table even though there are plenty rows in cd with itemIDs thats not in loans
now i was under the understanding that the left join should preserv the righthandside and fill the columns from the lefthandside with null values but this does not seem to be the case, can anbyone enlighten me?