Mysql将行和列从max到min排列并在php代码中显示排名?

时间:2013-05-25 11:37:12

标签: php mysql sql

我想制作一些可以完成某些工作的代码。

我有一个包含列和行的表格,我需要从表格中取出所有最大值,并将它们从高到低排列。

例如:

enter image description here

并在php文件中显示如下:

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 ...

2 个答案:

答案 0 :(得分:2)

尝试:

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

答案 1 :(得分:0)

你可以试试这个

 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

然后在PHP代码中只需按顺序获取它 例子

    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