[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Table Joins - recordcount from 2nd Table (mySQL)

Posted on 2008-01-27
2
Medium Priority
?
186 Views
Last Modified: 2010-03-20
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!
0
Comment
Question by:Maxafi
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 total points
ID: 20754859
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
 

Author Comment

by:Maxafi
ID: 20755591
That did the job perfectly, thankyou!

I really appreciate the swift response too, a real lifesaver!
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we’ll look at how to deploy ProxySQL.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question