我有一个表的列名为“ IS_QUICK”,键入TINYINT(0 | 1);
我需要计算在一段时间内他为真(1)和假(0)的次数:
在MySQL中:
ids = request.POST.getlist("id")
P_350 = request.POST["P_350"]
P_450 = request.POST["P_450"]
for id in ids:
fp_item = fp.objects.get(id=id)
items=[P_350,P_450]
for index, w in enumerate(items):
if index == 0:
if len(w) > 1:
fp_item.P_350 = w
fp_item.save()
elif index == 1:
if len(w) > 1:
fp_item.P_450 = w
fp_item.save()
但是他返回第3天到第6天之间所有IS_QUICK的计数,而不仅仅是返回第3天(例如)
编辑:表格:
<td style="display:none;">
<input name="id" type="text" value={{ field.id }} >
</td>
<td width="650">
{{ field.FP_Item }}
</td>
{% if field.P_350|length == 0 %}
<td style="display:none;">
{% else %}
<td>
{% endif %}
<input name="P_350" type="text" value={{ field.P_350 }} >
</td>
{% if field.P_450|length == 0 %}
<td style="display:none;">
{% else %}
<td>
{% endif %}
<input name="P_450" type="text" value={{ field.P_450 }} >
</td>
答案 0 :(得分:2)
要对布尔值TINYINT
进行计数,您可以简单地使用SUM(field)
来计数1,或使用SUM(NOT field)
来计数0。
SELECT
SUM(IS_QUICK) AS QUICK,
SUM(NOT IS_QUICK) AS NOT_QUICK
FROM qp1_relatorio_quickview rq
WHERE rq.created_at BETWEEN '2018-07-03 00:00:00' AND '2018-07-06 23:59:59';
上面的查询将选择该期间的总计。例如,如果要每天汇总一次,请确保选择并按所有相关字段分组:
SELECT
YEAR(rq.created_at) as ANO,
MONTH(rq.created_at) as MES,
DAY(rq.created_at) as DIA,
SUM(IS_QUICK) AS QUICK,
SUM(NOT IS_QUICK) AS NOT_QUICK
FROM qp1_relatorio_quickview rq
WHERE rq.created_at BETWEEN '2018-07-03 00:00:00' AND '2018-07-06 23:59:59'
GROUP BY ANO, MES, DIA;
答案 1 :(得分:0)
您好,您可以尝试使用sum
SELECT
sum(CASE WHEN rq1.IS_QUICK= 1 THEN 1 ELSE 0 END) as IS_QUICK,
sum(CASE WHEN rq1.IS_QUICK= 0 THEN 1 ELSE 0 END) as NOT_QUICK,
, YEAR(rq.created_at) as ANO
, MONTH(rq.created_at) as MES
, DAY(rq.created_at) as DIA
, HOUR(rq.created_at) as HORA
FROM qp1_relatorio_quickview rq WHERE rq.created_at BETWEEN '2018-07-03 00:00:00' AND '2018-07-06 23:59:59'