我尝试按月计算售出的数量,并得到一个数组。
$q = SELECT COUNT(id) AS January FROM tableName WHERE status='sold' AND month = '1'
UNION
SELECT COUNT(id) AS February FROM tableName WHERE status='sold' AND month = '2'
$row = mysqli_fetch_array(mysqli_query($connection, $q));
print_r($row);UNION不工作,当我尝试print_r(数组)时,我得到结果
[Jenuary] => 200如何在两个月内获得一个数组?
我想要结果:
[January] => 200,
[February] => 221发布于 2020-09-16 20:38:04
我认为您需要条件聚合:
select sum(month = 1) as january, sum(month = 2) as february
from tablename
where status = 'sold' and month in (1, 2)这只生成一行,其中有两列,分别名为january和february,每列都包含该月的行数。
或者,您可能希望每个月都有一行。在这种情况下,您可以使用简单的聚合:
select month, count(*) cnt
from tablename
where status = 'sold' and month in (1, 2)
group by month发布于 2020-09-16 21:07:52
您可以尝试此查询以获得所需的确切结果。
select MONTHNAME(STR_TO_DATE(month, '%m')) as month, count(*) cnt
from tablename
where status = 'sold'
group by month仅适用于特定月份
select MONTHNAME(STR_TO_DATE(month, '%m')) as month, count(*) cnt
from tablename
where status = 'sold' and month in(1, 2)
group by monthhttps://stackoverflow.com/questions/63920234
复制相似问题