Solved

MySQL Rank, Update, Select Order By Help

Posted on 2011-02-18
5
929 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 108

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 108

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now