I am wanting to create a stored procedure that accepts the team name and year and returns the number of players in that particular team for that particular year. I am working with MYSQL and listed below are the tables I am using:
Person (personID, name, phone, email, year)
Player (personID, dateOfBirth, school)
playerTeam (personID, teamID)
Team (teamID, teamName, ageGroup)
So far I have tried joining the tables and couting the personID only where teamName and Person.year = the input parameters but to no avail. Any ideas?
After submitting this question, using trial and error, I was able to get this to work using the following statements:
DELIMITER //
CREATE PROCEDURE teamSize (IN team VARCHAR (30), IN YEAR INT)
BEGIN
SELECT
COUNT(Person.personID) NumOfPlayers
FROM
Person AS p,
Player,
playerTeam,
Team
WHERE Person.personID = Player.personID
AND Player.personID = playerTeam.personID
AND Team.teamID = playerTeam.teamID
AND Team.teamName = team
AND Person.year = YEAR ;
END //
DELIMITER ;
CALL teamSize('Pirates', 1991);
Is there anyway to simplify this using joins etc as at the moment its a bit messy.
I’m not sure if you want like this:
but i think this is want you want.
Usage: