我使用下面的查询来计算“期间”列中具有不同值的记录数量。 Period列包含“Breakfast”,“Lunch”或“Dinner”,公式的输出按月列分组,然后每行显示一行:
TTL型。反馈:早餐:XXX
TTL型。反馈:午餐:XXX
TTL型。反馈:晚餐:XXX
现在,我需要的是每个总数的变量(请参阅早餐,午餐和晚餐返回的$ mydata ['total']。当前变量仅返回每个不同值的总和,并且不允许我专门针对个人期间分数。
<?
$whereParts = array();
if ($property) {
$whereParts[] = "Holidex = '$property' ";
}
$whereParts = array();
if ($outlet) {
$whereParts[] = "Outlet = '$outlet' ";
}
if ($pickyear) {
$whereParts[] = "YEAR(Date) = '$pickyear' ";
}
$sql = ("select distinct period from FB_Feedback_Card_Data");
//BUILD THE FINAL QUERY
if (count($whereParts) > 0) {
$sql .= " WHERE " . implode('AND ', $whereParts);
}
//count($whereParts) > 0
$result = $mysqli->query($sql) or die('<p>Query to get case Period data from FB_Feedback_Card_Data table failed:' . mysql_error() . '</p>');
while ($row = mysqli_fetch_row($result))
{
$period = $row[0];
//echo "$status <br>";
$mydata = stats("period", "$period", "$property", "$outlet", "$pickyear",$mysqli);
?>
<tr>
<td class="result_desc"><?echo "Ttl. Feedback : $period";?></td>
<td class="result"><? echo $mydata['a']; ?></td>
<td class="result"><? echo $mydata['b']; ?></td>
<td class="result"><? echo $mydata['c']; ?></td>
<td class="result"><? echo $mydata['d']; ?></td>
<td class="result"><? echo $mydata['e']; ?></td>
<td class="result"><? echo $mydata['f']; ?></td>
<td class="result"><? echo $mydata['g']; ?></td>
<td class="result"><? echo $mydata['h']; ?></td>
<td class="result"><? echo $mydata['i']; ?></td>
<td class="result"><? echo $mydata['j']; ?></td>
<td class="result"><? echo $mydata['k']; ?></td>
<td class="result"><? echo $mydata['l']; ?></td>
<td class="result_ytd"><strong><? echo $mydata['total']; ?></strong></td>
</tr>
<?
// end total status stats
}
?>
答案 0 :(得分:0)
你可以使用php函数array_sum这样就可以得到$ mydata变量的总和。
<td class="result_ytd"><strong><?= array_sum($mydata); ?></strong></td>
唯一的问题是,数组中一定不能有任何其他值,但我认为这就是这种情况。
答案 1 :(得分:0)
你不需要使用DISTINCT
,你需要GROUP BY
,这将把所有相同值的条目放在同一条记录中,然后你只需要计算每个他们:
SELECT period, count(period) as period_count FROM FB_Feedback_Card_Data WHERE ... GROUP BY period