?
Solved

MySQL Rank, Update, Select Order By Help

Posted on 2011-02-18
5
Medium Priority
?
945 Views
Last Modified: 2012-06-21
I would like to update a database ranking in 1 swoop instead of ranking and then updating.

Here is my rank implementation:
SET @rank=0;
SELECT @rank:=@rank+1 AS rank, id, fd1, fd2, fd3 FROM statsmsmobsters ORDER BY fd2 DESC, fd3 DESC;

I tried to do this and I get 0 results:
SET @rank=0;
UPDATE dbTable SET rank_fd= @rank:=@rank+1 ORDER BY fd2 DESC, fd3 DESC;

Thanks for any help!
0
Comment
Question by:PiZzL3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 21

Accepted Solution

by:
K V earned 2000 total points
ID: 34931724
I'm not sure what do you mean by " I get 0 results: " as the query should work proper and will update all the data under dbTable.
0
 
LVL 41

Expert Comment

by:Sharath
ID: 34932566
try this.
update statsmsmobsters 
join (select id,fd1,fd2,fd3,@curRank := @curRank + 1 AS rank 
        from statsmsmobsters 
        JOIN      (SELECT @curRank := 0) r 
     order by fd2 desc,fd3 desc) ranks 
on (ranks.id = statsmsmobsters.id) 
set statsmsmobsters.rank_fd = ranks.rank ;

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 34933192
Regarding this, "I get 0 results" -- why do you think that you get zero results?  How does your code test for the results?  Please post a complete code segment that shows the PHP you used, thanks.
0
 

Author Comment

by:PiZzL3
ID: 34933281
@theGhost_k8
I get this back from phpMyAdmin when I run the "update" query (the select works perfectly fine).

"Your SQL query has been executed successfully
# MySQL returned an empty result set (i.e. zero rows).".

You did just make me think of something though... I ran the following query:
"UPDATE dbTable SET rankfd= 0;"

then ran:
"SET @rank=0;
UPDATE dbTable SET rankfd= @rank:=@rank+1 ORDER BY fd2 DESC, fd3 DESC;"

and it worked as you said it would. I suppose it returned 0 results because none of the ranks were changing.

@Sharath_123
I'm currently having difficultly following that example.. I don't have a clue how it works. However my original update statement does work, so I don't need to use it anymore, but ty.

@Ray_Paseur
I wasn't using a script to test this, I was just in phpMyAdmin. I find it easier to test statements before I use them in my code that way. I got 0 results due to the reason stated above.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 34933625
Yep - the mysql_affected_rows() function does not care how many rows are matched, only how many were actually updated.  If you tell MySQL to SET myvalue="X" WHERE key < 10000, the resulting count will only represent the number of rows where myvalue was not already "X" and had to be changed.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question