我选择开始日期和结束日期,订单状态,然后我列出了我的表格中的订单。
最后一栏表示该日期有多少订单。我想得到一个数字,根本就有多少订单。我的意思是:从2017-01-01到2017-01-31,共有127个订单。
如果我们使用我的照片,结果将是49顺序。
SELECT datum, COUNT(rendeles_id) AS ennyi FROM rendeles_adatok
WHERE datum >= '$date_start' AND (datum <= '$date_end' AND status = $rendeles_allapot)
GROUP BY datum ORDER BY $kereses_sorrend
完整代码
$date_start = mysqli_real_escape_string($kapcs, $_POST['date_start']);
$date_end = mysqli_real_escape_string($kapcs, $_POST['date_end']);
$rendeles_allapot = mysqli_real_escape_string($kapcs, $_POST['rendeles_allapot']);
$kereses_sorrend = mysqli_real_escape_string($kapcs, $_POST['kereses_sorrend']);
if($rendeles_allapot != 1234 )
{
$sql = "
SELECT datum, COUNT(rendeles_id) AS ennyi FROM rendeles_adatok
WHERE datum >= '$date_start' AND (datum <= '$date_end' AND status = $rendeles_allapot)
GROUP BY datum ORDER BY $kereses_sorrend
";
}
else
{
$sql = "
SELECT datum, COUNT(rendeles_id) AS ennyi FROM rendeles_adatok
WHERE datum >= '$date_start' AND datum <= '$date_end'
GROUP BY datum ORDER BY datum ASC
";
}
//printr($sql);
echo '<table class="table table-hover table-bordered list">';
echo '<thead>
<tr>
<td class="left">Kezdő dátum</td>
<td class="left">Záró dátum</td>
<td class="center">Vásárlások száma</td>
</tr>
</thead>';
echo '<tbody>';
$hirek_down = mysqli_query($kapcs, $sql) or die(mysqli_error($kapcs));
$talalatok = mysqli_num_rows($hirek_down);
$fetch_count = mysqli_fetch_assoc($hirek_down);
if($talalatok > 0 )
{
echo '<p>Összesen '.$fetch_count['ennyi'].' találat.</p>';
echo '<p>Total sum: </p>';
while($a = mysqli_fetch_assoc($hirek_down))
{
echo '<tr>
<td class="left">'.html($a['datum']).'</td>
<td class="left">'.html($a['datum']).'</td>
<td class="center">'.html($a['ennyi']).'</td>
</tr>';
}
echo '</tbody>';
echo '</table>';
}
答案 0 :(得分:1)
您必须使用SUM()来获取该列的总和。
SELECT datum, SUM(rendeles_id) AS ennyi FROM rendeles_adatok
WHERE datum >= '$date_start' AND (datum <= '$date_end' AND status = $rendeles_allapot)
GROUP BY datum ORDER BY $kereses_sorrend
COUNT()只会计算与搜索匹配的行数,而SUM()会添加指定列的值。
答案 1 :(得分:1)
您的查询将如下所示:
SELECT datum, COUNT(rendeles_id) AS num_rows,
SUM(num_orders) as num_orders
FROM rendeles_adatok
WHERE datum >= '$date_start' AND (datum <= '$date_end' AND status = $rendeles_allapot)
GROUP BY datum
ORDER BY $kereses_sorrend;
注意:每天订单的计数通常不会被称为_id(至少用英语和我熟悉的其他语言)。我不知道您的列名称是什么,但您想要sum()
。