Solved

MySQL Rank, Update, Select Order By Help

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

943 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

5 Experts available now in Live!

Get 1:1 Help Now