2013-05-25 31 views
2

我想做一些代碼來做一些事情。Mysql排名從最大到最小的行和列,並顯示在PHP代碼中的排名?

我得到了一個列和行的表,我需要從表中取出所有最大的表並將它們從高到低排列。

例如:

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

+1

把它拿來我們需要更多的信息:其中位於表中的數據?您是否正在嘗試讀取HTML表格並將其結果加載到數組中,該數組將按照您之後描述的方式進行處理? –

+0

更大的是誰擁有更大的第一個元素,例如PD = 4000是第三個定位和LSI = 4000是第一個 –

回答

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 
+0

非常感謝你,這是主意,但我想另一件事,我怎麼能做第一個值: (1114,12,1,444,7,444) ,要分配2/3/4/5/6等數字嗎?例如12/2 = 6和6/3 = 2 ...並使第二行的值?所以,我把pd,ps等的第一個值和那些自動分攤的 –

+0

對不起,我不明白你想要什麼。你可以編輯你的問題,並添加你想要的結果或你想要的並解釋它好嗎? –

+0

我在想爲dtb做數學運算的值 –

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 
+0

表中是mysql,表名「partite」,元素是PS,PS,LSI,FRD, AK,PR和數字是爲每個元素不同 –