简体   繁体   中英

how would I alter this mysql query to include another mysql table?

I asked a question before about creating a table on the fly from a list of football results, what I forgot to add and realised later was that there was something missing, basically I want to include a club badge in the output table.

This is the code that creates the table on the fly that outputs in the browser

$sqlCommand = " 
    select team, 
    count(*) played,
    count(case when goalsfor > goalsagainst then 1 end) wins, 
    count(case when goalsagainst> goalsfor then 1 end) lost, 
    count(case when goalsfor = goalsagainst then 1 end) draws,
    sum(goalsfor) goalsfor, 
    sum(goalsagainst) goalsagainst, 
    sum(goalsfor) - sum(goalsagainst) goal_diff,
    sum(case when goalsfor > goalsagainst then 3 else 0 end 
        + case when goalsfor = goalsagainst then 1 else 0 end)score 
   from (select hometeam team, homebadge badge, goalsfor, goalsagainst from serie_a_2010_2011
   union all
         select awayteam, awaybadge, goalsagainst, goalsfor from serie_a_2010_2011) a
   group by team
   order by score desc, goal_diff desc";

I have created another table in my database called badges with the following structure

     ----------------------------------------------
    | id | team    |    badge                      |
     ----------------------------------------------
    |  1 | Inter   | (link to image of Inter badge)|
     ----------------------------------------------
    |  2 | Milan   | (link to image of Milan badge)|
     ----------------------------------------------
etc etc

this includes all the teams that will be output in the standings table

How would I create/add/join/ the extra query that will output the right badge for the right team?

I would say add badges_table.badge in the selected fields list (right after select)

And then add LEFT JOIN badges_table ON badges_table.id = tableThatHasBadge.badgeField right before the GROUP and this should get you going :)

let me know if you still have concerns

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM