bitscn.com
mysql那些事儿之(五)操作时间 一、时间 命令:select curdate(); 功能:以‘yyyy-mm-dd’的格式返回今天日期值。 命令:select curtime(); 功能:以‘hh:mm:ss’的格式返回时间。 命令:select now(); 功能:以‘yyyy-mm-dd hh:mm:ss’的格式返回时间。 二、自动记录时间 timestamp 列类型可以自动的标记 insert update的操作的时间。 举例: 创建表 create table student( id int, english tinyint, math tinyint, chinese tinyint, time timestamp ); 插入数据: insert into student(id,english,math,chinese) values(1,12,23,45); 查询数据: select * from student; 结果: --------------------------------------------------------------------- id | english | math | chinese | time | --------------------------------------------------------------------- 1 | 12 | 23 | 45 | 2012-10-20 10:34:09 --------------------------------------------------------------------- time列自动加入了数据insert 的时间。update 同样会影响数据的time列的时间值。 timestamp 的存储需求是4个字节;datetime存储需求是8个字节。 三、比较时间 select to_days(date); 给出一个日期date,返回从0年开始算的天数(到date为止)。 举例: select to_days(now())-to_days('2012-10-19'): 返回 1 天。 select to_days(now()); 返回 735161 天。即从现在到0年为止的天数。 bitscn.com