Solved

MySQL Rank, Update, Select Order By Help

Posted on 2011-02-18
5
937 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
5 Comments
 
LVL 21

Accepted Solution

by:
theGhost_k8 earned 500 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 40

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 109

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 109

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

777 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