如何将这些SQL SELECT查询合并到一个SELECT语句中

时间:2010-03-03 20:42:52

标签: mysql syntax select union

如何将这两个select语句组合成一个查询:

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

所以,我希望返回查询,例如:

fire_neighborhoods  fires  adw_neighborhoods  adw
xyzNeighborhood     6      abcNeighborhood    22
jklNeighborhood     3      tuvNeighborhood    40

我想简单地结合上面两个查询的结果。这两个查询彼此独立。一个结果不会影响其他查询的结果。我只需要一种方法将两个结果合二为一。

如果有人有任何建议,请告诉我。

谢谢。

-Laxmidi

5 个答案:

答案 0 :(得分:3)

您给出的示例表明您希望水平组合查询,但后来您声明它们完全独立。这些是冲突的陈述,因为您通常在记录彼此相关时水平地组合数据。下面是我将它们水平组合的想法,但我也注意到我将它们垂直地组合在下面的想法。

这取决于您想要如何链接它们。如果您基于邻域进行查询,则可以在fire_neighborhoods = adw_neighborhoods上的两个较大查询之间进行连接,例如:

SELECT fire_neighborhoods, fires, adw
FROM (

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

) AS fires
    INNER JOIN (  

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

) AS adw
    ON fires.fire_neighborhoods = adw.adw_neighborhoods

这只是一个例子。您可能需要一个不同的联接或其他东西来使它适合您。

现在,您声明这两个查询是独立的,不会相互影响。如果它们确实没有共同点,则应该为每个查询添加一个列,指示它来自哪个查询(例如,为fire查询添加一个常量值为1的列,为adw添加一个常量值为2的列查询)。然后,将UNION这两个大型查询放在一起。这将它们以垂直方式组合而不是横向方式。

答案 1 :(得分:2)

如果我理解你的需要,你可以将查询联合在一起,添加一列让你知道它来自哪个查询:

SELECT SUM( incidents )  AS fires_or_adw, neighborhoods AS fire_or_adw_neighborhoods, 'Fires' as which_query
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

UNION

SELECT SUM( incidents )  AS fires_or_adw, neighborhoods AS fire_or_adw_neighborhoods, 'ADW' as which_query
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

当然,你有两倍的行数,一半你的数据将为空,但只是“抨击”结果,因为你的例子并不常见。

答案 2 :(得分:1)

其他人使用鉴别器列给出的答案看起来就像你所追求的那样,但为了以防万一,可以在工会中添加虚拟占位符列,如下所示:

SELECT 
    SUM( incidents )  AS fires, 
    neighborhoods AS fire_neighborhoods,
    0 as adw,
    '' as adw_neighbourhoods
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 

UNION 

SELECT 
    0 as fires,
   '' as fire_neighbourhoods,
    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

答案 3 :(得分:1)

感谢您帮助解决这个问题。向David Hall,Aaron,Jeffrey Whitledge& NYSystemsAnalyst。我选择了虚拟列选项:

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 

答案 4 :(得分:0)

并排显示两个不相关的查询通常是报告软件而不是SQL引擎的工作。这里的问题是你要求查询工具做一些不适合做的事情:显示格式。

一种关系(从根本上讲,它应该是SELECT的结果)是一种关于世界状态的断言的数据结构。每一行都定义了一系列(理想情况下)为真的命题。

在你的问题中,这些行包含任意事实的相互关联,这些事实彼此无关,并且没有任何命题可以陈述。