Table Joins - recordcount from 2nd Table (mySQL)

Joins are something I've never been able to get my head around, and this one in particular I'm really struggling with.

I have 2 tables - merchants and promotions.  I need to list all merchants, then look for the number of promotions offered by each.  Some merchants may not have any at all (in which case the value returned should be 0).

This is the code I currently have;

SELECT r.retailer_name, r.id, d.merchant_id, count(d.merchant_id) as d_cnt
FROM merchants r
LEFT JOIN promotions d
ON d.merchant_id=r.id
WHERE r.retailer_name LIKE 'A%'
ORDER BY r.retailer_name ASC

So, the end result is that I want the Merchant Name, and a count of the number of promotions alongside.  I feel as though the above code is reasonably close, but I'm getting the following error;

'Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause'

I've tried a few things, but no success.

Help would be much appreciated!
MaxafiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
try this:
SELECT r.retailer_name, r.id, count(d.merchant_id) as d_cnt
FROM merchants r
LEFT JOIN promotions d
  ON d.merchant_id=r.id
WHERE r.retailer_name LIKE 'A%'
GROUP BY r.id, r.retailer_name
ORDER BY r.retailer_name ASC

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MaxafiAuthor Commented:
That did the job perfectly, thankyou!

I really appreciate the swift response too, a real lifesaver!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.