某表有ID 字段 和日期字段datetime格式的我取得ID为100的日期值假设这值是 2009-09-08 12:12:12我要去的去年同月分的一个格式化后的日期  也就是2008-9或2008-09有什么函数可以实现?PS:为什么MYSQL 算在“其他数据库开发”

解决方案 »

  1.   

    date_add('2009-09-08 12:12:12',INTERVAL -1 year)
      

  2.   

    date_format(
    date_add('2009-09-08 12:12:12',INTERVAL -1 year),'%Y-%m')
      

  3.   


    mysql> select concat(year("2009/09/08")-1,"-0",month("2009/09/08"));
    +-------------------------------------------------------+
    | concat(year("2009/09/08")-1,"-0",month("2009/09/08")) |
    +-------------------------------------------------------+
    | 2008-09                                               |
    +-------------------------------------------------------+
    1 row in set (0.00 sec)
      

  4.   

    用date_sub 比较准确。mysql> select date_format(date_sub('2009-09-08 12:12:12',interval 1 year),'%Y-%m
    ');
    +----------------------------------------------------------------------+
    | date_format(date_sub('2009-09-08 12:12:12',interval 1 year),'%Y-%m') |
    +----------------------------------------------------------------------+
    | 2008-09                                                              |
    +----------------------------------------------------------------------+
    1 row in set (0.00 sec)mysql>
      

  5.   

    add 是应该加1年 但加的是-1  所以减一年sub 是减1年  如果减的是-1  那就是加一年喽?
      

  6.   


    这个不用猜,MYSQL的文档中有具体说明。MySQL官方文档 http://dev.mysql.com/doc/refman/5.1/zh/index.html