2011-10-21 52 views
0
myTable 

field1 | field2 
------------------------ 
47  | 46,43,22,88,99,12 
22  | 12,99 
88  | 77 
12  | 99,22,84,5 

算使用PHP正在尋找從我的分貝(mysql的5.x的)拉從字段1具有在FIELD2最值2倍的值。這是Joomla/JomSocial,如果它很重要,但任何答覆非常感謝。獲得前兩名(切片?)中field1的從爆炸和場2

The out put of the following query should be 
Top Member ID = 47 
Top Member ID = 12 

我都試過,但沒有happend:

$query = 'SELECT * FROM myTable; 

    $db->setQuery($query); 
    $row = $db->loadObjectList();   
    $counted = array_count_values($row[field2]); 
    arsort($counted); 
    $top_two = array_slice($counted, 0, 2); 
    foreach ($top_two as $data) 
     { 
      echo "Top Member ID = " . $data ."<BR />"; 
     } 

非常感謝您

回答

2

可以在直SQL做到這一點:

SELECT field1, field2, LENGTH(field2)-LENGTH(REPLACE(field2,',','')) AS num FROM myTABLE ORDER BY num DESC LIMIT 2; 

的2行返回意志是你想要的。

+0

哇,你們尖叫'快....我thnk我喜歡這個論壇。現在將它插入我的腳本並嘗試一下...謝謝! – makenoiz

+0

ctcherry,是計數field2中元素的個數還是字符串長度? – makenoiz

+0

啊......計算逗號。好主意。 – sberry