2013-11-21 191 views
2

我有3個表: 協會(ID,姓名) 賽(ID,姓名) 統計(ID,日期,associationId,typeid的,數量)爲什麼MySQL告訴我該列不存在?

我的目標是讓所有關聯的列表,並對於所有關聯,類型列表(包括數量)按照首先排序,關聯關係的總數,然後針對每個類型排序。 對於這一點,我有這個疑問:

SELECT association.name AS associationName, 
     race.name AS raceName, 
     SUM(report.quantity) AS quantity, 
     subrequest.totalquantity as totalquantity 
    FROM stats report 
    JOIN association ON association.id = report.associationId 
    JOIN race ON race.id = report.raceId 
    JOIN (
     SELECT SUM(report.quantity) AS totalquantity, 
      association.id AS subId 
     FROM stats report 
     JOIN association ON association.id = report.associationId 
     WHERE date LIKE "2013-11-%" 
     GROUP BY association.id 
     ORDER BY totalquantity DESC 
    ) subrequest ON subrequest.subId = association.id 
    WHERE date LIKE "2013-11-%" 
    GROUP BY association.id, race.id 
     ORDER BY totalquantity DESC, quantity DESC 

這是工作完全正常!但是現在,我想只爲每個協會提供兩種最大的類型。我嘗試這樣做:

set @num := 0, @association := ''; 
select associationName, raceName, quantity, totalquantity, 
@num := if(@association = associationName, @num + 1, 1) as row_number, 
     @association := associationName as dummy 
FROM (
    SELECT association.name AS associationName, 
     race.name AS raceName, 
     SUM(report.quantity) AS quantity, 
     subrequest.totalquantity as totalquantity 
    FROM stats report 
    JOIN association ON association.id = report.associationId 
    JOIN race ON race.id = report.raceId 
    JOIN (
     SELECT SUM(report.quantity) AS totalquantity, 
      association.id AS subId 
     FROM stats report 
     JOIN association ON association.id = report.associationId 
     WHERE date LIKE "2013-11-%" 
     GROUP BY association.id 
     ORDER BY totalquantity DESC 
    ) subrequest ON subrequest.subId = association.id 
    WHERE date LIKE "2013-11-%" 
    GROUP BY association.id, race.id 
    ORDER BY totalquantity DESC, quantity DESC 
) x 
WHERE row_number <= 2 

有了這個,我有一個錯誤:#1054 - 在「where子句」未知列「ROW_NUMBER」如果我刪除WHERE子句,它工作正常,我有結果如預期。我也有一個正確的數字列row_number。

我必須這樣做,使其工作:

set @num := 0, @association := ''; 
select associationName, raceName, quantity, totalquantity 
FROM (
    select associationName, raceName, quantity, totalquantity, 
    @num := if(@association = associationName, @num + 1, 1) as row_number, 
      @association := associationName as dummy 
    FROM (
     SELECT association.name AS associationName, 
      race.name AS raceName, 
      SUM(report.quantity) AS quantity, 
      subrequest.totalquantity as totalquantity 
     FROM association_race_report report 
     JOIN association ON association.id = report.associationId 
     JOIN race ON race.id = report.raceId 
     JOIN (
      SELECT SUM(report.quantity) AS totalquantity, 
       association.id AS subId 
      FROM association_race_report report 
      JOIN association ON association.id = report.associationId 
      WHERE date LIKE "2013-11-%" 
      GROUP BY association.id 
      ORDER BY totalquantity DESC 
     ) subrequest ON subrequest.subId = association.id 
     WHERE date LIKE "2013-11-%" 
     GROUP BY association.id, race.id 
     ORDER BY totalquantity DESC, quantity DESC 
    ) x 
)y 
WHERE row_number <= 2 

這工作得很好!但我不明白爲什麼我需要添加另一個級別的請求來使WHERE子句工作?這真讓我很煩,因爲我添加了無用的查詢級別。有沒有辦法讓它工作?爲什麼MySQL提出這個錯誤?

謝謝!

+1

我終於找到了解決方案!如果(@association = associationName,@num + 1,1)<= 2,我只需要將WHERE row_number <= 2替換爲WHERE – user3017110

回答

0

MySQL不喜歡列引用,因爲它在查詢的選擇部分被定義 - 它在連接和where子句中都沒有用。

這些解決方案,我看到你自己發現的,都是用定義它的表達式替換別名,或者創建一箇中間子查詢以避免重寫整個事物。 (查詢優化器會正常刪除中間查詢。)

相關問題