我有一个数据库名称“d”并且有一个表名称“t”... t 有两列 id 和月份...
我必须计算每个月的 id 数量。
实际上,我正在 $monthyear 中进行输入,现在我想将前 25 个月的 count(id) 存储在 $row 中
我使用这种类型的命令
$search = "SELECT * FROM `d`.`t` where id>2";
$result = mysqli_query($con, $search);
$row = mysqli_fetch_array($result);
请告诉我一个可以实现此技巧的查询,并且可以像这样存储它
$row[0]="no of ids in the inputed month"
$row[1]="no of ids in the [inputed month - 1 month]"
$row[2]="no of ids in the [inputed month - 2 month]"
等等......
注意:月份和“年”都很重要。
最佳答案
如果不是所有月份都有 ID,那么您需要使用派生表。像这样的事情:
SELECT t.monthyear, COALESCE(COUNT(s.monthyear),0) as numOfID
FROM(SELECT '012016' as monthyear
UNION ALL
SELECT '022016'
....) t
LEFT JOIN YourTable s
ON(t.monthyear = s.monthyear)
GROUP BY t.monthyear
关于php - 分别计算过去 25 个月中的每个月 (*),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46444540/