Web Master
Web Master

Reputation: 55

Mysql rank rows and columns from max to min and show the rank in php code ?

I want to make a code that does the some things.

I have got a table with columns and rows and i need to take from the table all max from the table and rank them from high to low.

For example :

enter image description here

and to show it in php file like this :

1- PS , 2- PD , 3- PS , 4- LSI , 5- PD , 6- PS , 7- FRD , 8- LSI , 9- PD , 10- PS , 11- AK , 12- FRD , 13- LSI , 14 PD , 15- PS etc ...

Upvotes: 2

Views: 376

Answers (2)

echo_Me
echo_Me

Reputation: 37233

you can try this

 Select cod,val
From
(
 Select 'pd' as cod, pd as val FROM Table1
union all
Select 'lsi' as cod, lsi as val FROM Table1
union all
Select 'val' as cod,frd as val FROM Table1
union all
Select 'ak' as cod,ak as val FROM Table1
union all
Select 'ps' as cod,ps as val FROM Table1
union all
Select 'pr' as cod, pr as val FROM Table1
) x
order by val desc

DEMO HERE

then in php code just fetch it with ordering example

    for($i=1 ; $i<= mysql_num_rows($query) ; $i++){  // or you can use count(*) in your query
    echo $i.'-'.$row['cod'] ;
    }
  //not tested but its this idea

Upvotes: 0

user359040
user359040

Reputation:

Try:

select type, @curRank := @curRank + 1 AS rank, amount
from (SELECT @curRank := 0) r
cross join
(select 'PD' type, PD amount from mytable union all
 select 'LSI' type, LSI from mytable union all
 select 'FRD' type, FRD from mytable union all
 select 'AK' type, AK from mytable union all
 select 'PS' type, PS from mytable union all
 select 'PR' type, PR from mytable) v
order by amount desc

Upvotes: 2

Related Questions