I have the following query:
SET @year1 = 2011;
select B.YearID, sum(B.ab), sum(B.h), sum(B.h)/ sum(B.ab)
From Batting B join Pitching P
on (P.playerID = B.playerID and B.yearID = P.yearid and B.ab > 0 )
where B.yearid = @year1
This works fine to retrieve results for one year. But can I do a loop using on mySQL that would allow me to execute the query and see results for each year?
This code does not work and produces syntax errors:
SET @year1 = 2011;
While @year1 <= 2014 DO
SET @year1 = @year1 + 1;
select B.YearID, sum(B.ab), sum(B.h), sum(B.h)/ sum(B.ab)
From Batting B join Pitching P
on (P.playerID = B.playerID and B.yearID = P.yearid and B.ab > 0 )
where B.yearid = @year1;
End While
Can I write a query in MySQL or must I use a language such as PHP?