查询缺少的月份,我们没有卖出任何东西? [英] Query missing months in which we haven't sold anything?
问题描述
我的查询是:
$sql="SELECT COUNT(`Variant`) AS tsold, MONTHNAME(`sold_date`) AS mname FROM `vehicle_sold` GROUP BY MONTH(`sold_date`) ";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result)) {
echo $row["mname"],"---", $row["tsold"],"<br />";
}
这给了我以下结果:
January---1
February---2
March---7
April---11
May---6
July---1
6月没有销售,因此我想要的是查询返回"June --- 0"的查询.如果还显示到12月的接下来的几个月,那还可以.我想要以下输出:
There are no sales in June so what I want is the query to return "June---0" for example. If it also shows the next months up to December it's ok. I'd like the following output:
January---1
February---2
March---7
April---11
May---6
June---0
July---1
Aug---0
Sept---0
Oct---0
Nov---0
Dec---0
推荐答案
我假设如果您不出售任何东西,则数据库中实际上没有数据.因此,在没有信息的情况下很难生成一个月.
I assume you have actually no data in the database if you did not sell anything. Hence its a bit hard to generate a month without info.
您想要一个包含所有月份的数组,对应于销售额. 我建议您做这样的事情:
You want an array with all the months, corresponding with the amount of sales. I would suggest you make something like this:
准备一个带有月份的数组,默认情况下所有值为0(您可以使该数组列表为"nicer",但仅作为示例).
Prepare an array with the months with all the values on 0 as default (you can make this array list 'nicer', but just now as example).
$data['March'] = 0;
$data['April'] = 0;
现在,您可以像以前一样进行迭代
Now you can iterate like you did
while($row = mysqli_fetch_array($result)) {
$data[$row["mname"]] = $row["tsold"];
}
如果数据库未填充它,则该值仍将为0.否则它将被数据库值覆盖.
If it does not get filled by the database, the value would still be 0. Otherwise it gets overwritten by the database value.
这篇关于查询缺少的月份,我们没有卖出任何东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!