Search code examples
mysqlsubqueryzipcodepostal-code

Mysql Subquery - Need to make a connection between two like queries in order to Count Occurrences of Postal Codes and Zip Codes


I'm having a bit of an issue figuring this out. I have two queries that I need to put together and no matter what I've tried I always end up with an error in mysql syntax. The two queries are below.

Query 1 - this is for canadian postal codes

SELECT ta.city, ta.email_address, ta.country_code, COUNT(*)
FROM jos_form_submitteddata_form1 tb
JOIN jos_postalzip_redirect ta ON UPPER(LEFT(tb.FIELD_24, 3)) = LEFT(ta.postal_zip, 3)
WHERE LENGTH(tb.FIELD_24) > 5
GROUP BY ta.city;  

Query 2 - This if for US zip codes

SELECT ta.city, ta.email_address, ta.country_code, COUNT(*)
FROM jos_form_submitteddata_form1 tb
JOIN jos_postalzip_redirect ta ON UPPER(tb.FIELD_24) = LEFT(ta.postal_zip, 5)
WHERE LENGTH(tb.FIELD_24) <= 5
GROUP BY ta.city
HAVING ta.country_code = 'US';

The goal is to have both of these queries display in the same table with correct counts


Solution

  • Try using a UNION statement.

    http://dev.mysql.com/doc/refman/5.0/en/union.html