PHP 中的自动生存
如果我有这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是想对
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).