Queries of MySQL database queries

0

I have this table

The teams are related to another table called teams. Containing: name (primary key), stadium, city and foundation. .

With these two tables I would like to draw a classification of the teams with their points. Example: Real Madrid 20, Athletic 19.

Finding out with local goals and visitors. Example if crazy goals > visiting goals; Local Team has 3 points.

    
asked by Borja Sanchez 18.12.2016 в 14:55
source

1 answer

0

You can try this, to add the points that each local team gets as a visitor:

 SELECT 
    @curRank := @curRank + 1 AS rank,
    Equipo,sum(puntos) as puntos FROM (
        select Equipo_local as Equipo,
        sum(CASE WHEN Goles_local>Goles_visitante then 3 WHEN Goles_local=Goles_visitante then 1 else 0 END) as puntos
        from equipo
        Group by Equipo_local
        UNION
        SELECT Equipo_visitante as Equipo,
        sum(CASE WHEN Goles_local<Goles_visitante then 3 WHEN Goles_local=Goles_visitante then 1 else 0 END) as puntos
        from equipo
        Group by Equipo_visitante) t,(SELECT @curRank := 0) r
 group by t.Equipo
    
answered by 11.02.2017 / 22:48
source