Mysql 计算过期的&;每个唯一id的未过期行
我有一张这样的桌子:Mysql 计算过期的&;每个唯一id的未过期行,mysql,sql,Mysql,Sql,我有一张这样的桌子: | id | certificate|expires | | 1 | 123 |12-DEC-15 | | 1 | 123 |12-DEC-09 | | 1 | 123 |13-DEC-09 | | 2 | 456 |14-DEC-16 | | 2 | 456 |14-DEC-09 | | 2 | 789
| id | certificate|expires |
| 1 | 123 |12-DEC-15 |
| 1 | 123 |12-DEC-09 |
| 1 | 123 |13-DEC-09 |
| 2 | 456 |14-DEC-16 |
| 2 | 456 |14-DEC-09 |
| 2 | 789 |14-DEC-09 |
| 2 | 789 |14-DEC-09 |
|id | expired | unexpired |
|1 |2 |1 |
|2 |3 |1 |
我尝试对每个用户已过期和未过期的证书数量进行求和,如下所示:
| id | certificate|expires |
| 1 | 123 |12-DEC-15 |
| 1 | 123 |12-DEC-09 |
| 1 | 123 |13-DEC-09 |
| 2 | 456 |14-DEC-16 |
| 2 | 456 |14-DEC-09 |
| 2 | 789 |14-DEC-09 |
| 2 | 789 |14-DEC-09 |
|id | expired | unexpired |
|1 |2 |1 |
|2 |3 |1 |
我已经统计了所有过期的证书,但它只显示了未过期的行数。如何获取过期证书的数量
SELECT
id,
COUNT(certificate) as numcerts,
expires
FROM
certificates
WHERE
expires > current_date()
GROUP BY id
having numcerts > 0
ORDER BY COUNT(*) DESC
如果日期存储在
date
数据类型中,则下面的查询将给出结果
select
id,
sum( case when expires < curdate() then 1 else 0 end ) as expires,
sum( case when expires > curdate() then 1 else 0 end ) as unsepired
from certificates group by id
您通过
expires>current\u date()
使用STR\u TO\u date将这些文本明确筛选出来,以将其转换为正确的日期。非常感谢。这个答案非常完美。