troubleshooting Question

MySQL: How do I show the count of two tables in one query.

Avatar of AdrianSmithUK
AdrianSmithUK asked on
MySQL ServerDatabases
9 Comments1 Solution410 ViewsLast Modified:
I have 2 queries below and I would like to join them to give a single result.

Table 1 has more postcodes than table 2 and I would like to list the super-set of all postcodes in the left hand column and if table 2 doesn't have a result for that particular postcode it would show zero.

Here is an example of the desired output.

Postcode    Count1    Count2
SW                200          57
LS                 400           0
PH                 250           23


Can you help?

SELECT `postcode`, COUNT( * ) AS Count1 FROM  `table1` GROUP BY  `postcode`;
SELECT `postcode`, COUNT( * ) AS Count2 FROM  `table2` GROUP BY  `postcode`;
Pratima Pharande

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 9 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros