Jump to content

Recommended Posts

Hi Guys,

 

Say I enter some football results in my results database:

 

team_a 1-1 team_c

team a 2-1 team_b

team_c 1-2 team_b

 

results

home_team

home_score

away_team

away_score

form

 

and to work out the form I run this query:

 

   // Calculations for form guide
if($home_score > $away_score) {
$form='home win';
    } else if($home_score < $away_score) {
$form='away win';
    }else {
$form='draw';
    }

 

Say I wanted to work out the form for each team ie team_a, team_b ,team_c how could I do it in a query?  Or can anyone think of a better way of record and calculate each team's form?

 

Thanks

 

Chris

Link to comment
https://forums.phpfreaks.com/topic/58822-creating-a-recent-form-guide/
Share on other sites

Hi Mr Chris,  the SQL Forum would have been a better place for this post. But, did you know mysql can do IF statements? IF statements take three arguments:  the conditional, what to do if true, what to do in false.

 

# Let's that that right now your query is,
select home_team, home_score, away_team, away_score from `games` where gameid=1;

# You can derive a "form" within your query with an IF statement, in this case as the 5th column.
select 
      home_team, 
      home_score, 
      away_team, 
      away_score,
      if( home_score=away_score , 'draw',
                    if (home_score>away_score, 'home_win' , 'away_win')
        ) as form
from `games` where gameid=1;

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.