如何将这些SQL SELECT查询合并到一个SELECT语句中 [英] How do I Combine these SQL SELECT queries into one SELECT statement
问题描述
如何将这两个选择语句组合成一个查询:
How do I combine these two select statements into one query:
SELECT SUM( incidents ) AS fires, neighborhoods AS fire_neighborhoods
FROM (
SELECT *
FROM `fires_2009_incident_location`
UNION ALL SELECT *
FROM `fires_2008_incident_location`
UNION ALL SELECT *
FROM `fires_2007_incident_location`
UNION ALL SELECT *
FROM `fires_2006_incident_location`
) AS combo
GROUP BY fire_neighborhoods ORDER BY fires DESC
SELECT SUM( incidents ) AS adw, neighborhoods AS adw_neighborhoods
FROM (
SELECT *
FROM `adw_2009_incident_location`
UNION ALL SELECT *
FROM `adw_2008_incident_location`
UNION ALL SELECT *
FROM `adw_2007_incident_location`
UNION ALL SELECT *
FROM `adw_2006_incident_location`
) AS combo2
GROUP BY adw_neighborhoods ORDER BY adw DESC
因此,我希望查询返回,例如:
So, I'd like the query to return, something like:
fire_neighborhoods fires adw_neighborhoods adw
xyzNeighborhood 6 abcNeighborhood 22
jklNeighborhood 3 tuvNeighborhood 40
我想简单地合并上面两个查询的结果.这两个查询彼此独立.一个查询的结果不会影响另一个查询的结果.我只需要一种方法就可以将两个结果放在一起.
I want to simply combine the results of the two queries above. The two queries are independent of each other. The results of one doesn't effect the results of the other query. I simply need a way to slam the two results together into one.
如果有人有任何建议,请告诉我.
If anyone has any advice, please let me know.
谢谢.
-Laxmidi
推荐答案
感谢您帮助我们解决这一问题.向大卫·霍尔,亚伦,杰弗里·惠特里奇& NYSystems分析师.我选择了虚拟列选项:
Thanks for the the help in figuring this out. Shout out to David Hall, Aaron, Jeffrey Whitledge & NYSystemsAnalyst. I went with the dummy column option:
SELECT SUM( incidents ) , neighborhoods, 'adw' as offense
FROM (
SELECT *
FROM `adw_2009_incident_location`
UNION ALL SELECT *
FROM `adw_2008_incident_location`
UNION ALL SELECT *
FROM `adw_2007_incident_location`
UNION ALL SELECT *
FROM `adw_2006_incident_location`
) AS combo
GROUP BY neighborhoods
UNION ALL
SELECT SUM( incidents ), neighborhoods, 'fire' as offense
FROM (
SELECT *
FROM `fire_2009_incident_location`
UNION ALL SELECT *
FROM `fire_2008_incident_location`
UNION ALL SELECT *
FROM `fire_2007_incident_location`
UNION ALL SELECT *
FROM `fire_2006_incident_location`
) AS combo2
GROUP BY neighborhoods
这篇关于如何将这些SQL SELECT查询合并到一个SELECT语句中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!