0

我知道有很多与我类似的问题的帖子,但我似乎无法找到适用于我的案例的解决方案,如下所示。

我添加了空列,以便列数匹配,并且我知道我需要在第一个选择中说明所有列。问题就在于此。我不知道如何在第一个选择中包含一个与第二个选择中的 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
4

2 回答 2

0

我猜你还想NULL在你的第一个SELECT语句中放置一个对应COUNT于第二个SELECT语句中的列的列。

就像在您的第二个语句中一样,在您的第一个NULL列中添加一个列,应该没问题。并且不要忘记列的 ,因为使用第一个 的列名。SELECTSELECTALIASUNIONSELECT

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'

但是,您需要NULL在第二个中添加另一列以与第SELECT一个中的列数匹配SELECT。希望这会帮助你。

于 2013-05-29T10:52:58.080 回答
0

我意识到我正在用错误的方法来解决这个问题。最初我以为我不能用 COUNT 作为选择 1 的一部分来选择 2。事实证明这是可能的,经过一番挣扎后我设法完成了它。

感谢您的输入。请参阅下面的解决方案:

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
于 2013-05-29T15:48:12.890 回答