這裏是我們當前的查詢:Mysql的傳遞變量
SELECT info.yearID, info.nameFirst, info.nameLast, info.HR
FROM (
SELECT a.yearID, a.nameFirst, a.nameLast, a.HR FROM (
SELECT @curRank := @curRank+1 as rank, i.nameFirst, i.nameLast, i.masterID, i.HR, i.yearID
FROM ( SELECT (select @curRank := 0), m.nameFirst, b.yearID, m.nameLast, b.masterId, sum(b.HR) as HR
FROM master as m left join batting as b on b.masterID = m.masterID
WHERE b.yearID = 1882 group by b.masterID)
as i ORDER BY i.HR desc
)a
WHERE a.HR = (SELECT i.HR
FROM (
SELECT (select @curRank := 0), m.nameFirst, b.yearID, m.nameLast, b.masterId, sum(b.HR) as HR
FROM master as m left join batting as b on b.masterID = m.masterID
WHERE b.yearID = 1882
group by b.masterID
)
as i ORDER BY i.HR desc limit 1)) as info ;
我們需要讓這個1882年的不硬編碼的,而是從1871年老話 - 2013年
我不是在尋找的答案,只是一個正確的方向。
如果我是做起來很@yearVar可能是從另一個查詢的值,你會怎麼做呢? –
你是不是通過一些外部手段傳遞@curRank?使用一個循環來經歷你想要使用的一組年。 – FirebladeDan