2015-10-15 55 views
0

這裏是我們當前的查詢: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年

我不是在尋找的答案,只是一個正確的方向。

回答

0
b.yearID = @yearVar 

^^ @符號將被解析,您對待@curRank的方式會類似。這是提供某種類型的動態值的手段。

+0

如果我是做起來很@yearVar可能是從另一個查詢的值,你會怎麼做呢? –

+0

你是不是通過一些外部手段傳遞@curRank?使用一個循環來經歷你想要使用的一組年。 – FirebladeDan

0

只是將其更改爲:

WHERE b.yearID BETWEEN 1871 AND 2013 
+0

這不會解決他的硬編碼問題 – FirebladeDan