Solved

MySQL Rank, Update, Select Order By Help

Posted on 2011-02-18
5
943 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:
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 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 110

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 110

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

690 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