我需要从每列收集计数,每行有多个结果。问题是列是复制第一列的值。
$sql="SELECT MODEL,
sum(CLICK) AS LOCCLICK, sum(VIEW) AS LOCVIEW, sum(EMAIL) AS LOCEMAIL, sum(SHARE) AS LOCSHARE
FROM STAT
WHERE ID = ".$id."
GROUP BY MODEL";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$clickcoun = $row['LOCCLICK'];
$viewcoun = $row['LOCVIEW'];
$emailcoun = $row['LOCEMAIL'];
$sharecoun = $row['LOCSHARE'];
$model = $row['MODEL'];
echo "\n";
// Log the stats into the daily updates.
$sqlx = 'INSERT INTO DAILY (ID, QDATE, VIEWS, CLICKS, EMAILS, SHARES, MODEL)
VALUES ('.$id.', NOW(), '.$viewcoun.', '.$clickcoun.', '.$emailcoun.', '.$sharecoun.', "'.$model.'" )';
$mysqli->query($sqlx);
}
} else {
echo "0 results"; die();
}
我要找的结果如下:
Ford:views = 302,emails = 21,shares = 10
Dodge:views = 567,emails = 41,shares = 13
雪佛兰:views = 324,电子邮件= 23,shares = 9
我想将这些输入到每日统计数据细分中,以便我可以查询它们将它们放入图表并显示其每日覆盖面。在本月底,我打算以同样的方式再次打破这些月度概述。
答案 0 :(得分:0)
您不需要循环来执行此操作。只需使用insert . . . select
:
INSERT INTO DAILY (ID, MODEL, CLICKS, VIEWS, EMAIL, SHARES, QDATE)
SELECT $id, MODEL, sum(CLICK) AS LOCCLICK, sum(VIEW) AS LOCVIEW,
sum(EMAIL) AS LOCEMAIL, sum(SHARE) AS LOCSHARE, now()
FROM STAT
WHERE ID = ".$id."
GROUP BY MODEL;