从爆炸中获取 field1 中的前两个(切片?)并在 field2 中进行计数

发布于 2024-12-10 20:09:58 字数 807 浏览 0 评论 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)中提取 field1 中在 field2 中具有最多值的 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 />";
        } 

非常感谢

myTable

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

Using PHP am looking to pull from my db (Mysql 5.x) the 2 values from field1 that have the most values in field2. this is for Joomla/JomSocial if it matters but any reply is greatly appreciated.

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

I have tried this but nothing 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 />";
        } 

Thank you very much

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝海似她心 2024-12-17 20:09:58

您可以直接使用 SQL 执行此操作:

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

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

You can do this in straight SQL:

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

The 2 rows returned will be the ones you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文