MySQL查询Where和GROUP BY

myzjeezk  于 2022-12-10  发布在  Mysql
关注(0)|答案(1)|浏览(160)

我有一个数据库表,在状态字段中包含Payed、Pending、Draft、Expired和Success。如何按日期、所有状态和过期状态分组。
我有这样的疑问

SELECT count(*) as total,
    date_format(insert_date,'%d-%m-%Y') as `a`,  status
FROM transaction
WHERE insert_date BETWEEN '2022-11-24 00:00:00' AND '2022-12-08 23:59:59'
GROUP BY DATE_FORMAT(insert_date, '%d'), status

如何将1个日期内的所有状态和过期状态分组?例如,在日期04-12-2022中,必须为TOTAL=19 STATUS ALL且TOTAL=8 STATUS EXPIRED。

z9smfwbn

z9smfwbn1#

可以将TotalExpired放在同一行的不同列中。

SELECT date_format(insert_date, '%d-%m-%Y') AS Date,
    COUNT(*) AS Total,
    SUM(status = 'EXPIRED') AS expired
FROM transaction
WHERE insert_date BETWEEN '2022-11-24 00:00:00' AND '2022-12-08 23:59:59'
GROUP BY a

相关问题