我有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提出這個錯誤?
謝謝!
我終於找到了解決方案!如果(@association = associationName,@num + 1,1)<= 2,我只需要將WHERE row_number <= 2替換爲WHERE – user3017110