Store Names

March 26, 2009

Mysql-How to use Count and Join to return count of related records, but also show all unrelated recs ungrouped?

mysql
Design Is Everything asked:


Two dbases - Stores and Registrations. Each registration record is related to the Stores by a Store ID. I’m trying to get a count how many registration records there are per store. So a simple Group with would suffice:

select count(*) as thecount, store.id, store_name, registration_store_id, from STORES left join REGISTRATION on store.id = registration_store_id Group by registration_store_id

BUT, what I want is to also display ALL of the Store names regardless of whether they have a related record in the registration database. The problem is that with the Group by statement I’m simply left with a single row that is a count of ALL the stores with no related record (a NULL value in the registration_store_id.

How can I do this?

Filed under Programming & Design by administrator

Permalink Print 1 Comment