我想做一个代码来做一些事情。
我有一个包含列和行的表,我需要从表中取出所有最大值并将它们从高到低排列。
例如 :
并将其显示在 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 ...
尝试:
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
你可以试试这个
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
然后在 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