如何实现此COUNT(日期> NOW())AS计数?

时间:2013-12-14 09:21:51

标签: mysql date count

我有一张包含实时旅行日期列表的表格,我想每年对它们进行分组,但也会创建一个新列,其中包含今年的剩余日期:

  year   |  count /*of remaining dates*/
  --------------------
  2014   |  12
  2013   |  0
  2012   |  0
  2010   |  0

我尝试了这个但是不起作用:

SELECT YEAR(`date`) AS year, COUNT(`date` > NOW()) AS count
FROM live
GROUP BY YEAR(`date`) 
ORDER BY YEAR(`date`) DESC

1 个答案:

答案 0 :(得分:2)

以这种方式试试

SELECT YEAR(`date`) AS year, SUM(`date` > CURDATE()) AS count
  FROM live
GROUP BY YEAR(`date`) DESC

示例输出:

| YEAR | COUNT |
|------|-------|
| 2013 |     2 |
| 2012 |     0 |

这是 SQLFiddle 演示