Mysql 子查询 - 需要在两个类似查询之间建立连接,以便计算邮政编码和邮政编码的出现次数
我在解决这个问题时遇到了一些问题。我有两个查询需要放在一起,无论我尝试什么,我总是会遇到 mysql 语法错误。这两个查询如下。
查询 1 - 这是针对加拿大邮政编码的查询
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;
查询 2 - 这是针对美国邮政编码的查询
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';
目标是让这两个查询以正确的计数显示在同一个表中
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用 UNION 语句。
http://dev.mysql.com/doc/refman/5.0/en /union.html
Try using a UNION statement.
http://dev.mysql.com/doc/refman/5.0/en/union.html
UNION 不能满足您的需要?
A UNION doesn't do what you need?