2013-05-29 106 views
0

我知道在我的類似問題中有很多帖子,但我似乎無法找到適用於我的案例的解決方案,如下所示。mysql UNION與COUNT

我添加了空列,以便列數匹配,並且我知道我需要在第一個選擇中指定所有列。這就是問題所在。我不知道如何在第一個選擇中包含COUNT列,以便與第二個選擇中的COUNT對應。

感謝您的協助。

首先選擇:

SELECT brands.brand_id, 
     brands.brand, 
     models.model_id, 
     models.model, 
     segments.segment_id, 
     segments.segment, 
     versions.version_id, 
     versions.version, 
     versions.places, 
     versions.gearbox, 
     versions.doors, 
     prices.price 
FROM versions 
     INNER JOIN models USING (model_id) 
     INNER JOIN segments USING (segment_id) 
     INNER JOIN brands USING (brand_id) 
     INNER JOIN prices USING(version_id) 
WHERE price BETWEEN 200001 AND 225000 
     AND brands.active = 'Y' 
     AND models.active = 'Y' 
     AND versions.active = 'Y' 

第二選擇:

SELECT Count(*) AS SafetyItems, 
     version_id, 
     NULL  AS COL3, 
     NULL  AS ...., 
     NULL  AS COL12 
FROM versiontrim 
     INNER JOIN trims USING(trim_id) 
     INNER JOIN versions USING(version_id) 
     INNER JOIN prices USING(version_id) 
     INNER JOIN models USING (model_id) 
     INNER JOIN brands USING (brand_id) 
WHERE trimtype IN('sec', 'help') 
     AND price BETWEEN 200001 AND 225000 
     AND brands.active = 'Y' 
     AND models.active = 'Y' 
     AND versions.active = 'Y' 
GROUP BY version_id 

示例結果的第一選擇:

brand_id brand model_id model segment_id version_id  price 
    58 Renault 11  Megane  4   44   209900 
    58 Renault 14  Scenic  5   54   209900 
    58 Renault 11  Megane  4   69   200900 
    71 Toyota  29  Yaris  2   214   200900 
    71 Toyota  30  Auri   4   216   207900 
    52 Nissan  58  Pick-up  14   282   209000 
    24 Ford  21  Focus  4   290   209000 

的第二示例結果選擇,我想在上面附加到(後價格欄):

SafetyItems  version_id 
    9    44 
    7    54 
    9    69 
    10    214 
    6    216 
    1    282 
    10    290 
+0

能不能請你把一些樣本數據http://sqlfiddle.com –

+0

嗨德瓦爾,我看着sqlfiddle.com,但我不知道如何將場景添加爲以上它。我會看看我能做些什麼。 – BernardA

+0

看看http://sqlfiddle.com/#!2/d0dc7/16/0把你的SQL語句 –

回答

0

我猜你也想提出一個NULL列在你的第一個SELECT聲明將在第二SELECT聲明對應的COUNT列。

只需在您的第一個SELECT中插入一個NULL列,就像在您的第二個SELECT聲明中一樣,那應該沒問題。並且不要忘記列的ALIAS,因爲UNION使用第一個SELECT的列名稱。

SELECT 
NULL AS SafetyItems, 
brands.brand_id, 
brands.brand, 
models.model_id, 
models.model, 
segments.segment_id, 
segments.segment, 
versions.version_id, 
versions.version, 
versions.places, 
versions.gearbox, 
versions.doors, 
prices.price 
FROM versions 
INNER JOIN models 
USING (model_id) 
INNER JOIN segments 
USING (segment_id) 
INNER JOIN brands 
USING (brand_id) 
INNER JOIN prices 
USING(version_id) 
WHERE price BETWEEN 200001 AND 225000 
AND brands.active='Y' 
AND models.active='Y' 
AND versions.active='Y' 

但是,你需要在第二SELECT再添NULL列匹配列數第一SELECT。希望這會幫助你。

+0

謝謝凱爾,但那不行。以上將選擇2被添加下面選擇1而不是作爲列被添加到選擇1,這是所需的結果。爲了清晰起見,我添加了每個選擇的樣本。 – BernardA

0

我意識到我正在用錯誤的方法去解決這個問題。起初,我以爲我不能做COUNT選擇2作爲選擇1的一部分。事實證明,這是可能的,我設法讓它在一些掙扎之後完成。

感謝您的意見。請參考下面的解決方案:

SELECT brands.brand_id, 
    brands.brand, 
    models.model_id, 
    models.model, 
    segments.segment_id, 
    segments.segment, 
    versions.version_id, 
    versions.version, 
    versions.places, 
    versions.gearbox, 
    versions.doors, 
    prices.price, 
    COUNT(trimtype) 
FROM versions 
    INNER JOIN models USING (model_id) 
    INNER JOIN segments USING (segment_id) 
    INNER JOIN brands USING (brand_id) 
    INNER JOIN prices USING(version_id) 
    INNER JOIN versiontrim USING(version_id) 
    INNER JOIN trims USING(trim_id) 
WHERE price BETWEEN 200001 AND 210000 
    AND trimtype IN('sec', 'help') 
    AND brands.active = 'Y' 
    AND models.active = 'Y' 
    AND versions.active = 'Y' 
GROUP BY version_id