GROUP BY,ORDER BY和Sub Query

时间:2015-09-27 11:52:22

标签: mysql group-by subquery sql-order-by

我已经搜索了这个,但没有设法让任何工作。

不确定这是否可行,但我会尽力解释......

我有几张桌子,小屋和分数 - 一对多,其中任何一个小屋可以有多个分数。

Lodges
LodgeID (PK)
Lodge
etc

Scores
ScoreID (PK)
LodgeID (FK)
Score
Comment

从这里,我有一个使用GROUP BY小屋的小屋列表,显示任何有任何分数的小屋,以及该小屋的平均分数。

这一切都很有效 - 但是,在Scores表中还有一个Comment字段,如果Lodge在Scores表中有任何记录并带有注释,我希望在这个列表中包含一个指向链接的链接。 / p>

我有它工作,但如果小屋没有收到任何评论,希望链接不显示。否则,人们会看到链接,点击它,可能没有任何评论。

因此,我认为问题是因为使用GROUP BY,可能会或可能不会显示包含评论的分数表中的记录。

我也尝试过使用ORDER BY,但这只是命令结果。或者使用WHERE,但只返回有评论的小屋。我认为HAVING可能是它,但那只是再次过滤了结果。

我正在寻找的是使用GROUP BY,但是对于汇总的小屋(而不是所有的结果)是按照注释的顺序,所以如果小屋有任何评论,我可以用它来显示或隐藏链接到对那个小屋的评论。

我希望这是有道理的,也许是可能的。

感谢。

这是我原来的疑问:

SELECT 
  scores.ScoreID, 
  COUNT(scores.ScoreID) as scoreCount, 
  AVG(scores.Score) as AverageScore, 
  lodges.LodgeID,   
  lodges.Lodge, 
  countries.Country, 
  lodges.CountryID, 
  categoriesFull.CategoryID, 
  nominations.NominationID, 
  scores.Comments, 
  nominations.AY_finalist, 
  nominations.AY_rank, 
  lodges.2016_Registered, 
  nominations.Year, 
  categoriesShort.CategoryShort, 
  countries.CountryShort
FROM 
  lodges 
     INNER JOIN continents 
        ON lodges.ContinentID = continents.ContinentID 
     INNER JOIN countries 
        ON lodges.CountryID = countries.CountryID 
     INNER JOIN scores 
        ON lodges.LodgeID = scores.LodgeID 
     INNER JOIN nominations 
        ON lodges.LodgeID=nominations.LodgeID 
        INNER JOIN categoriesFull 
           ON nominations.CategoriesFullID = categoriesFull.CategoryID 
          AND scores.categoryID = categoriesFull.CategoryID 
          INNER JOIN categoriesShort 
             ON categoriesFull.CategoryID = categoriesShort.CategoryShortID
WHERE 
      lodges.CountryID = 8 
  AND scores.CategoryID = 7 
  AND Year = '2016' 
  AND Nominee = 'Y' 
GROUP BY 
  Lodge

我一直在尝试子查询,但没有任何快乐:

SELECT * FROM
(
SELECT 
  scores.ScoreID, 
  COUNT(scores.ScoreID) as scoreCount, 
  AVG(scores.Score) as AverageScore, 
  lodges.LodgeID,   
  lodges.Lodge, 
  countries.Country, 
  lodges.CountryID, 
  categoriesFull.CategoryID, 
  nominations.NominationID, 
  scores.Comments, 
  nominations.AY_finalist, 
  nominations.AY_rank, 
  lodges.2016_Registered, 
  nominations.Year, 
  categoriesShort.CategoryShort, 
  countries.CountryShort 
FROM 
  lodges 
  INNER JOIN continents 
    ON lodges.ContinentID = continents.ContinentID 
  INNER JOIN countries 
    ON lodges.CountryID = countries.CountryID 
  INNER JOIN scores 
    ON lodges.LodgeID = scores.LodgeID 
  INNER JOIN nominations 
    ON lodges.LodgeID=nominations.LodgeID 
  INNER JOIN categoriesFull 
    ON nominations.CategoriesFullID = categoriesFull.CategoryID 
    AND scores.categoryID = categoriesFull.CategoryID 
  INNER JOIN categoriesShort 
    ON categoriesFull.CategoryID = categoriesShort.CategoryShortID
  WHERE 
  lodges.CountryID = 8 
  AND scores.CategoryID = 7 
  AND Year = '2016' 
  AND Nominee = 'Y'
ORDER BY Comments ASC)
  AS Comments 
GROUP BY 
  Lodge

上面只列出了一个带有ScoreCount的小屋作为该小组中所有小屋的所有分数的总数。

如果我将*更改为字段:

SELECT
  scores.ScoreID, 
  COUNT(scores.ScoreID) as scoreCount, 
  AVG(scores.Score) as AverageScore, 
  lodges.LodgeID,   
  lodges.Lodge, 
  countries.Country, 
  lodges.CountryID, 
  categoriesFull.CategoryID, 
  nominations.NominationID, 
  scores.Comments, 
  nominations.AY_finalist, 
  nominations.AY_rank, 
  lodges.2016_Registered, 
  nominations.Year, 
  categoriesShort.CategoryShort, 
  countries.CountryShort 
FROM
(
SELECT 
  scores.ScoreID, 
  COUNT(scores.ScoreID) as scoreCount, 
  AVG(scores.Score) as AverageScore, 
  lodges.LodgeID,   
  lodges.Lodge, 
  countries.Country, 
  lodges.CountryID, 
  categoriesFull.CategoryID, 
  nominations.NominationID, 
  scores.Comments, 
  nominations.AY_finalist, 
  nominations.AY_rank, 
  lodges.2016_Registered, 
  nominations.Year, 
  categoriesShort.CategoryShort, 
  countries.CountryShort 
FROM 
  lodges 
  INNER JOIN continents 
    ON lodges.ContinentID = continents.ContinentID 
  INNER JOIN countries 
    ON lodges.CountryID = countries.CountryID 
  INNER JOIN scores 
    ON lodges.LodgeID = scores.LodgeID 
  INNER JOIN nominations 
    ON lodges.LodgeID=nominations.LodgeID 
  INNER JOIN categoriesFull 
    ON nominations.CategoriesFullID = categoriesFull.CategoryID 
    AND scores.categoryID = categoriesFull.CategoryID 
  INNER JOIN categoriesShort 
    ON categoriesFull.CategoryID = categoriesShort.CategoryShortID
  WHERE 
  lodges.CountryID = 8 
  AND scores.CategoryID = 7 
  AND Year = '2016' 
  AND Nominee = 'Y'
ORDER BY Comments ASC)
  AS Comments 
GROUP BY 
  Lodge

我收到错误:

'字段列表'中的未知列'scores.ScoreID'

我是在吠叫正确的树吗?

非常感谢任何建议或指示。

0 个答案:

没有答案