Jquery中文网 www.jquerycn.cn
Jquery中文网 >  数据库  >  mysql  >  正文 关于mysql日期的一些例子

关于mysql日期的一些例子

发布时间:2014-07-20   编辑:www.jquerycn.cn
关于mysql日期的一些例子

MySQL中的月份计算
//减少一个月,比如:原来的subtime='2006-10-22 12:22:22' 减少后变成:'2006-9-22 12:22:22'
update message set subtime = DATE_SUB(subtime, INTERVAL 1 MONTH)

//增加一个月
update message set subtime = DATE_ADD(subtime, INTERVAL 1 MONTH)

//取出五天内有记录
select * from message where to_days(now())-to_days(subtime)<=5 and parentid=0;
//now()返回当前系统时间,日期格式为:0000-00-00 00:00:00;
//从MySQL中取中datetime类型的
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
String dateStr=sdf.format(rs.getString("subtime"));

Mysql的时间列自动插入当前日期时间
用current_timestamp,不过这个默认值只用在timestamp的列,对datetime列无效
例子:
create table default_time (
  id int not null primary key auto_increment,
  name varchar(20) default 'chenlb',
  my_time timestamp default current_timestamp
);
注意:一个表只能有一个timestamp列的默认值为当前日期时间。

MySQL语句中怎样获取当前系统日期:
select current_date;
        select current_time;
select current_timestamp;

mysql怎么计算两日期间隔是几天: SELECT datediff('日期1', '日期2');
MYSQL日期比较问题:
select   *
from   表
where year(日期字段名)=2007 and month(日期字段名)=6 and day(日期字段名)=10

您可能感兴趣的文章:
关于mysql日期的一些例子
mysql删除binlog日志及使用日志恢复数据的方法
MySQL日期函数详解
mysql实例 日期计算的存储过程
mysql实例 日期变量的二个例子
自动与手动清理mysql-binlog日志的方法
JavaScript Date(日期)对象
mysql获取当前时间的经典实例(2013最新版)
MYSQL启用日志,查看日志,利用Mysqlbinlog工具恢复MySQL数据库
利用Mysqlbinlog工具恢复MySQL数据库的例子

[关闭]