PHP 中的自动生存

发布于 2024-10-05 04:20:34 字数 1587 浏览 5 评论 0原文

如果我有这个 SQL 查询:

select substring(id for 2) as key, 
yw, count(*)
from pref_money group by yw, key

返回每周和每个键的用户数:

 key |   yw    | count
-----+---------+-------
 VK  | 2010-45 |   144
 VK  | 2010-44 |    79
 MR  | 2010-46 |    72
 OK  | 2010-48 |   415
 FB  | 2010-45 |    11
 FB  | 2010-44 |     8
 MR  | 2010-47 |    55
 VK  | 2010-47 |   136
 DE  | 2010-48 |    35
 VK  | 2010-46 |   124
 MR  | 2010-44 |    40
 MR  | 2010-45 |    58
 FB  | 2010-47 |    13
 FB  | 2010-46 |    13
 OK  | 2010-47 |  1834
 MR  | 2010-48 |    13
 OK  | 2010-46 |  1787
 DE  | 2010-44 |    83
 DE  | 2010-45 |   128
 FB  | 2010-48 |     4
 OK  | 2010-44 |  1099
 OK  | 2010-45 |  1684
 DE  | 2010-46 |   118
 VK  | 2010-48 |    29
 DE  | 2010-47 |   148

那么我该如何计算这些用户?我正在尝试:

    $sth = $db->prepare('select substring(id for 2) as key, yw, count(*)
                         from pref_money group by yw, key');
    $sth->execute();
    while ($row = $sth->fetch(PDO::FETCH_ASSOC))
            ++$users[$row['yw']][$row['key']];
    print_r($users);

但出现很多错误。

我正在尝试获取 堆叠条形图。 x 轴将显示周数,y 轴将显示按关键字符串分组的用户数。

谢谢你!亚历克斯

if I have this SQL query:

select substring(id for 2) as key, 
yw, count(*)
from pref_money group by yw, key

returning number of users per week and per key:

 key |   yw    | count
-----+---------+-------
 VK  | 2010-45 |   144
 VK  | 2010-44 |    79
 MR  | 2010-46 |    72
 OK  | 2010-48 |   415
 FB  | 2010-45 |    11
 FB  | 2010-44 |     8
 MR  | 2010-47 |    55
 VK  | 2010-47 |   136
 DE  | 2010-48 |    35
 VK  | 2010-46 |   124
 MR  | 2010-44 |    40
 MR  | 2010-45 |    58
 FB  | 2010-47 |    13
 FB  | 2010-46 |    13
 OK  | 2010-47 |  1834
 MR  | 2010-48 |    13
 OK  | 2010-46 |  1787
 DE  | 2010-44 |    83
 DE  | 2010-45 |   128
 FB  | 2010-48 |     4
 OK  | 2010-44 |  1099
 OK  | 2010-45 |  1684
 DE  | 2010-46 |   118
 VK  | 2010-48 |    29
 DE  | 2010-47 |   148

Then how can I please count those users? I'm trying:

    $sth = $db->prepare('select substring(id for 2) as key, yw, count(*)
                         from pref_money group by yw, key');
    $sth->execute();
    while ($row = $sth->fetch(PDO::FETCH_ASSOC))
            ++$users[$row['yw']][$row['key']];
    print_r($users);

but get numerous errors.

I'm trying to get the data for a stacked bars diagram. The x-axis will show the week numbers and the y-axis will show number of users, grouped by the key strings.

Thank you! Alex

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

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

发布评论

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

评论(2

吻风 2024-10-12 04:20:34

您是想对 count 列进行总计还是仅获取返回的行数?如果是后者,php 有一个方法。如果是前者,则需要改为 $users[$row['yw']][$row['key'] += $row['count'] (假设数组有已经创建了)。

Are you trying to total the count column or just get number of rows returned? If the latter, php has a method for that. if the former, you need to make it $users[$row['yw']][$row['key'] += $row['count'] instead (assuming the array has already been created).

森末i 2024-10-12 04:20:34
$sth = $db->prepare('select substring(id for 2) as key, yw, count(*)
                         from pref_money group by yw, key');
    $sth->execute();
    $users=array();  //  register $users as an array.
    while ($row = $sth->fetch(PDO::FETCH_ASSOC))
            if(!isset($users[$row['yw']])){// see if $users['whatever_key'] has already been set
               $users[$row['yw']]=0;//if not, initialize it as a default starting value of 0
             }
            $users[$row['yw']]+=$row['key'];//now we won't get any nasty notices here about undeclared variables or keys..
    print_r($users);
$sth = $db->prepare('select substring(id for 2) as key, yw, count(*)
                         from pref_money group by yw, key');
    $sth->execute();
    $users=array();  //  register $users as an array.
    while ($row = $sth->fetch(PDO::FETCH_ASSOC))
            if(!isset($users[$row['yw']])){// see if $users['whatever_key'] has already been set
               $users[$row['yw']]=0;//if not, initialize it as a default starting value of 0
             }
            $users[$row['yw']]+=$row['key'];//now we won't get any nasty notices here about undeclared variables or keys..
    print_r($users);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文