I am new to SQL. I am trying to create a function that returns the number of games won by a team. But when executing the function it gives me a syntax error and I don't see the error no matter how many times I try. This is the code:
delimiter //
create function partidos_ganados(equipo int) returns int
declare victorias_local int;
declare victorias_visitante int;
declare victorias_totales int;
set victorias_local = (select count(*) from partidos where puntosL > puntosV and elocal = equipo);
set victorias_visitante = (select count(*) from partidos where puntosV > puntosL and evisit = equipo);
set victorias_totales = (victorias_local + victorias_visitante);
return(victorias_totales);
end//
delimiter ;
the error says
FAIL:
create function games_won(team int) returns int declare home_wins int; declare wins_away int; declare total_wins int; set home_wins = (select count( ) from matches where LPs > VPs and ehome = team); set away_wins = (select count( ) from matches where VPoints > LPoints and evisit = team); set total_wins = (home_wins + away_wins); return(total_wins); end;
Message:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'declare wins_local int; declare wins_away int; declare victory' at line 1
Thanks in advance and sorry for the inconvenience.
You need to add the BEGIN