Jump to content

Recommended Posts

Much like the person who posted this topic, I am trying to find the ranking of a particular ID.

 

The issue I face is that the table has multiple score rows that must be sorted by year, summed and then ranked. I tried modifying the query suggested by kickstart but couldn't get it to work.

 

The table only has three important columns id points and season

 

CREATE TABLE `Results_tbl` (
`Race_ID` int(10) NOT NULL,
`Season` year(4) NOT NULL,
`Rider_ID` int(5) NOT NULL,
`Position` int(2) default NULL,
`Race_Time` time default NULL,
`MSecs` char(5) default NULL,
UNIQUE KEY `Race_ID_2` (`Race_ID`,`Rider_ID`),
KEY `Race_ID` (`Race_ID`),
KEY `Season` (`Season`),
KEY `Rider_ID` (`Rider_ID`)
) ENGINE=MyISAM AUTO_INCREMENT=7575 DEFAULT CHARSET=latin1

 

What I tried was this:

 

SELECT z.*, x.Rank
FROM `Results_tbl` z
INNER JOIN (SELECT a.Rider_ID, SUM(a.Points), @num := @num + 1 AS Rank from `Results_tbl` a, (SELECT @num := 0) d
order by SUM(a.Points)) x
ON z.Rider_ID = x.Rider_ID
WHERE z.Rider_ID = '1'
WHERE a.Season=2009
LIMIT 1

 

And that gave me a result bot it was completely wrong as it the rank was the same as the number of rows in the table.

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/170936-solved-ranking/
Share on other sites

Well, interestinly enough, the topic directly below mine when I posted this gave me the information I needed to come up with a solution on my own.

 

This is what I ended up with:

 

SELECT a.Rider_ID, a.Rank
FROM (

SELECT a.Rider_ID, SUM( a.Points ) AS Score, @num := @num +1 AS Rank
FROM `Results_tbl` a
WHERE a.Season =2009
GROUP BY a.Rider_ID
ORDER BY Score DESC
) AS a
WHERE a.Rider_ID =2 

Link to comment
https://forums.phpfreaks.com/topic/170936-solved-ranking/#findComment-901572
Share on other sites

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.