当前位置: > 数据库 > MySQL >

关于mysql日期的一些例子

时间:2014-11-23 15:43来源:linux.it.net.cn 作者:IT

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

 
(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容