user3298004
user3298004

Reputation: 195

Ordering data based on field value

I am having a problem ordering my data. I am using a simple query to try and order a table but it doesn't seem to be working. Once the table is ordered I want to update the first column so I am using the following:

$qry9 = "SELECT * FROM t1 ORDER BY Ranking DESC";
$qry10 = "UPDATE t1 SET Level = '1' LIMIT $number";

This doesn't work for some reason. The data is still ordered in the way it was originally entered in the table, so when the update is performed the first rows of the table are updated, not the rows with the highest ranking. Any help here would be greatly appreciated. Thanks in advance.

Upvotes: 0

Views: 48

Answers (2)

vhu
vhu

Reputation: 12788

$qry9 (SELECT * FROM t1 ORDER BY Ranking DESC";) doesn't sort anything in the table, it just sorts the result set of the select. Therefore if you want to apply $qry10 only for the first row, you need to use ORDER BY Ranking DESC there as well:

$qry10 = "UPDATE t1 SET Level = '1' ORDER BY Ranking DESC LIMIT $number";

Upvotes: 2

Gordon Linoff
Gordon Linoff

Reputation: 1269533

The order by applies to the result set of the query, not to the table itself. In MySQL, you can use an order by with update:

UPDATE t1
    SET Level = '1' 
    ORDER BY ranking DESC
    LIMIT $number;

Upvotes: 2

Related Questions