SELECT team, sum(score) AS totalscore FROM (SELECT team1_id AS team, sum(score1) AS score FROM xeg8u_bl_match GROUP BY team1_id UNION ALL SELECT team2_id AS team, sum(score2) AS score FROM xeg8u_bl_match GROUP BY team2_id) AS scores GROUP BY team ORDER BY totalscore DESC LIMIT 1
Wyciąga mi id i sumę goli drużyny która strzeliła ich najwięcej spośród wyników całej tabeli. Problem polega na tym że w tej tabeli bazy danych znajdują się wszystkie mecze, wszystkich sezonów i pucharów.
mam dwie tabele:
xeg8u_bl_match - gdzie zapisane są mecze wszystkich sezonów i pucharów.
id|m_id|team1_id|team2_id|score1|score2|match_descr|published|is_extra|m_played|m_date|m_time|m_location|bonus1|bonus2
xeg8u_bl_matchday - gdzie zapisane są dni meczowe których wspólnym elementem z xeg8u_bl_match jest pole id = m_id z tabeli powyżej, a kolumna s_id odpowiada za numer sezonu, pucharu.
id|m_name|m_descr|s_id|is_playoff
teraz próbowałem zmodyfikować zapytanie jak poniżej jednak nic mi nie wyświetla poza błędami sql.
SELECT md.id, m.m_id, team, sum(score) AS totalscore FROM xeg8u_bl_matchday AS md, xeg8u_bl_match AS m (SELECT team1_id AS team, sum(score1) AS score FROM xeg8u_bl_match GROUP BY team1_id UNION ALL SELECT team2_id AS team, sum(score2) AS score FROM xeg8u_bl_match GROUP BY team2_id) AS scores WHERE m.m_id=md.id GROUP BY team ORDER BY totalscore DESC LIMIT 1
Ma ktoś jakiś pomysł jak to zrobić?