我正在做一个项目,它有一个看起来像这样的数据库表(结构上,但不是数据上的 - puuh):
year | event | category
------------------------------------------------------------
1970 | Someone ate a cheeseburger | food
2010 | Justin bieber was discovered | other
1500 | Columbus makes 3rd trip to America | notable
------------------------------------------------------------
我如何查询此表,以便我的结果按世纪分组?
2000-century:
2010 - Justin bieber was discovered1900-century:
1970 - Someone ate a cheeseburger1500-century:
1500 - Columbus makes 3rd trip to America
对于俗气的伪数据感到抱歉:)
最佳答案
MySQL 没有世纪函数,但它有年份,所以您基本上可以这样做:
SELECT whatever
FROM yourtable
WHERE ...
GROUP BY CAST((Year(datetimefield) / 100) AS INTEGER)
当然,这没有考虑到世纪正式从“1”年开始,而不是从“0”年开始(例如,1900 年仍然是 18 世纪,而不是 19 世纪)。但是,如果您不追求精确,简单的“除以 100”方法是最快/最简单的。
关于mySQL:查询按世纪分组的与时间相关的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4937702/