我想要在过去1年或年份> 1年但创建年份为< 2的ID,等等。像想,如果是这样的表:mysql select datetime查询找到1岁,2岁等的ID
+----+--------------------------+---------------------+
| id | data | cur_timestamp |
+----+--------------------------+---------------------+
| 1 | The time of creation is: | 2014-02-01 20:37:22 |
| 2 | The time of creation is: | 2015-01-01 20:37:22 |
| 3 | The time of creation is: | 2015-02-01 20:37:22 |
| 4 | The time of creation is: | 2015-12-01 20:37:22 |
| 5 | The time of creation is: | 2016-02-01 20:37:22 |
| 6 | The time of creation is: | 2016-04-01 20:37:22 |
+----+--------------------------+---------------------+
我想获得一个表是这样的:
+-----------------------+-------+
| date range | count |
+-----------------------+-------+
| last 1 year | 3 |
| > 1 year & < 2 years | 2 |
| > 2 years | 1 |
+-----------------------+-------+
在第一列的任何内容都很好。但我想按照上面的规定计数。我尝试了各种各样的东西。
select count(*) from ids where cur_timestamp >= date_sub(now(), interval 1 year);
这会给我一个最近一年的ID数。同样,我可以扩展它来获得其他值。但是有什么方法可以在单个查询中获取所有值?
要么使用UNION与每一个内部或检查采用的解决方案用于轧制和 – frlan