Jump to content

Recommended Posts

hey guys i have two tables called teams and results and basically the the results table will store the results of each match the team has.

 

what i need to do is when i select all the teams i need the team with the most results to be ordered first and descending from there....example

 

------------------

man united  12 pts

liverpool      9 pts

etc etc

------------------

 

the tables im using are below if anyone could tell me the best way of calculating the results and ordering them please...thank you

 

teams

-------------

team_id

team_name

-------------

 

results

-------------

result_id

team_id

result

-------------

Link to comment
https://forums.phpfreaks.com/topic/207432-ordering-mysql-query-results/
Share on other sites

You'll want to join the two tables together using a simple MySQL Join, eg

SELECT t.team_id, 
       t.team_name,
       COUNT(r.results) AS team_points 
FROM teams t 
LEFT JOIN results r ON t.team_id = r.team_id
GROUP BY t.team_id
ORDER BY team_points DESC

The above is an untested query. Give it a go and see what you get.

A small change

SELECT t.team_id, 
       t.team_name,
       SUM(r.results) AS team_points, 
       SUM(r.win) AS team_wins, 
       SUM(r.loss) AS team_losses, 
       SUM(r.draw) AS team_draws,  
FROM teams t 
LEFT JOIN results r ON t.team_id = r.team_id
GROUP BY t.team_id
ORDER BY team_points DESC

I don't think his table has columns "win", "loss", or "draw".  Rather I think the table column "results" contain those values.

 

You may need a CASE for this to work.

 

UN-TESTED

SELECT t.team_id,
       t.team_name,
       COUNT(r.result_id) as 'team_points',
       COUNT(CASE r.results
       WHEN 'win' THEN 'team_wins'
       WHEN 'loss' THEN 'team_losses'
       ELSE 'team_draws' END)
FROM teams t
LEFT JOIN results r ON t.team_id = r.team_id
GROUP BY t.team_id
ORDER BY team_points DESC

ive been trying to work it out mysql and ive got it working but im not sure if this is the best way....im new to the case statement so this might be a bit long winded if you could help

 

SELECT t.team_id, t.team_name, COUNT( r.league_match_result_id ) AS 'team_matches_played', COUNT(
CASE r.result
WHEN 'Win'
THEN 'team_wins'
END ) AS 'team_wins', COUNT(
CASE r.result
WHEN 'Draw'
THEN 'team_draws'
END ) AS 'team_draws', COUNT(
CASE r.result
WHEN 'Loss'
THEN 'team_losses'
END ) AS 'team_losses'
FROM teams t
LEFT JOIN league_match_results r ON t.team_id = r.team_id
GROUP BY t.team_id
ORDER BY team_wins DESC, team_draws DESC

 

Try:

SELECT t.team_id, t.team_name, COUNT( r.league_match_result_id ) AS 'team_matches_played', COUNT(
CASE r.result
WHEN 'Win'
THEN 1
END ) AS 'team_wins', COUNT(
CASE r.result
WHEN 'Draw'
THEN 1
END ) AS 'team_draws', COUNT(
CASE r.result
WHEN 'Loss'
THEN 1
END ) AS 'team_losses'
FROM teams t
LEFT JOIN league_match_results r ON t.team_id = r.team_id
GROUP BY t.team_id
ORDER BY team_wins DESC, team_draws DESC

 

It might not make a difference, but it is correct, as you are wanting to count and not change the column.

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.