在field1中从爆炸中获得前两个(切片?)并在field2中计数

时间:2011-10-21 01:30:31

标签: php explode slice

myTable

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

使用PHP我希望从我的db(Mysql 5.x)中获取field1中具有最多值的field1中的2个值。这是Joomla / JomSocial如果重要,但任何回复都非常感谢。

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

我试过这个,但没有发生任何事情:

    $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 />";
        } 

非常感谢

1 个答案:

答案 0 :(得分:2)

您可以在直接SQL中执行此操作:

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

返回的2行将是您想要的行。