Apologies in advance if this is a really dumb question but I searched all over for a simple solution and failed…
I have a really simple query to see what grade of beds are available (bedStatusID = 5) in a hotel which works great
SELECT type AS bedType, COUNT(bedID) AS curCount
FROM bed
JOIN hotel on hotelID = bedHotelID
LEFT JOIN type ON typeID = bedTypeID
WHERE bedStatusID = 5 AND bedHotelID = 40 GROUP BY bedTypeID;
bedType curCount
premium 181
deluxe 22
standard 12
My probelm is that when there are no statdard beds available I get this:
bedType curCount
premium 181
deluxe 22
And I want to get this:
bedType curCount
premium 181
deluxe 22
standard 0
I’ve tried Coalesce, SUM, IFNULL etc but I must be getting the systax wrong as nothing seems to work…
Is there a way this can be achieved in MySQL?
You mean GROUP BY type, don’t you? I mean, bedTypeID is not in the list of fields that should be returned in the result and when you use GROUP BY, normally you don’t use the alises but the real name of the filed
@molona, Thanks for the fast response but my GROUP BY has no effect on what I’m trying to achieve. With the JOIN on type I can GROUP BY either but thanks for the tip on the Alias? I’ve tried all variations (alias, no aliad, type, bedTypeID, typeName etc) and the result is the same… I still don’t get ‘standard 0’ when there are no standard beds available…
@molona - Thanks again for your input but I’m afraid its not really helping me toward an answer. I’m reluctant to code a PHP workaround if there is something that can be done inside mySQL…
SELECT type.typeID AS bedType
, COUNT(bed.bedID) AS curCount
FROM type
LEFT OUTER
JOIN bed
ON bed.bedTypeID = type.typeID
AND bed.bedStatusID = 5
AND bed.bedHotelID = 40
GROUP
BY type.typeID;
notice you don’t actually need the hotel table for this query
also, please don’t stick the table name into the column name at the front – that’s a clumsy and overly verbose naming convention
when more than one table is in the FROM clause, you should use the table names to qualify the column names as shown above, instead of trying to distinguish them using that horrible naming convention
@r937 God bless ya Gov’… Haven’t seen it for a while now but Remember Eddie Yates… very well… A real character…With Stan and Ilda Ogden… My personal favourites… Watched it since the sixties…Never a dull moment…
on a side note r937 - As you are problably aware, I’m trying to learn MySQL and clearly struggling in places… Would it be acceptable to PM you with something in a hope for some guidance?