根据数十年计算数据

时间:2015-10-03 06:52:13

标签: mysql

我有一个表Books,其中包含一本书的发行年份列(类型:年份)。我想计算每十年发布的书籍数量。

我尝试了以下内容:

select count(*) as count, concat(decade, '-', decade + 9) as ReleaseYear
from (
    select floor('ReleaseYear' / 10) * 10 as decade
    from Book
) t
group by decade;

但它返回0结果。

当我尝试year('ReleaseYear')时,它会返回空值。

2 个答案:

答案 0 :(得分:1)

为每一行创建一个decade值,并按十年来查找计数(十年)。

<强>查询

select t.decade,count(t.decade) as BooksCount from
(
    select *,
    concat((releaseYear - ((releaseYear % 10)-1)),'-',((releaseYear - ((releaseYear % 10)-1))+ 9)) as decade
   from Books
)t
group by t.decade;

SQL Fiddle

答案 1 :(得分:0)

尝试;

SELECT 
    count(*) `count`,
    concat(FLOOR(year(`ReleaseYear`)/10)*10, '-', FLOOR(year(`ReleaseYear`)/10)*10  +  9) `ReleaseYear`
from `books`
group by FLOOR(year(`ReleaseYear`)/10)*10

演示 sqlfiddle