2

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

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
Fred B.
  • 45
  • 3

2 Answers2

0

A UNION doesn't do what you need?

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
UNION
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 AND ta.country_code = 'US' GROUP BY ta.city
Leslie
  • 3,604
  • 7
  • 38
  • 53
0

Try using a UNION statement.

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

Paul Sonier
  • 38,903
  • 3
  • 77
  • 117