-> DATE_FORMAT(NOW(), '%m-%d' ) A
看看执行是否正常.
正常的话, 就把 NOW() 替换为你表里面的字段名字。 后面再 FROM 你的表。
第二个参数:
%W 星期名字(Sunday……Saturday)
%D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)
%Y 年, 数字, 4 位
%y 年, 数字, 2 位
%a 缩写的星期名字(Sun……Sat)
%d 月份中的天数, 数字(00……31)
%e 月份中的天数, 数字(0……31)
%m 月, 数字(01……12)
%c 月, 数字(1……12)
%b 缩写的月份名字(Jan……Dec)
%j 一年中的天数(001……366)
%H 小时(00……23)
%k 小时(0……23)
%h 小时(01……12)
%I 小时(01……12)
%l 小时(1……12)
%i 分钟, 数字(00……59)
%r 时间,12 小时(hh:mm:ss [AP]M)
%T 时间,24 小时(hh:mm:ss)
%S 秒(00……59)
%s 秒(00……59)
%p AM或PM
%w 一个星期中的天数(0=Sunday ……6=Saturday )
%U 星期(0……52), 这里星期天是星期的第一天
%u 星期(0……52), 这里星期一是星期的第一天
%% 一个文字“%”。
所有的其他字符不做解释被复制到结果中。
我们在向表中插入数据的时候,如果表字段有类似于创建时间的字段,往往需要手动添加,特别的麻烦。我们只需要把时间字段设置成 timestamp 类型,然后把默认值设置为 CURRENT_TIMESTAMP 即可。这样在添加一条新数据的时候,该字段会自动生成当前时间,不需要再手动添加,非常的方便。mysql>create table timetest(->id int not null,
->modtime timestamp default current_timestamp on update current_timestamp,
->primary key(id)
->)engine=innodb,default charset=utf8
Query OK, 0 rows affected (0.06 sec)
以下是测试,包括建表时记录时间,插入时记录时间,更新时记录时间:
mysql>insert into timetest(id) values (1),(2),(3)
Query OK, 3 rows affected (0.06 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql>select * from timetest
+----+---------------------+
| id | modtime |
+----+---------------------+
| 1 | 2011-09-21 09:56:24 |
| 2 | 2011-09-21 09:56:24 |
| 3 | 2011-09-21 09:56:24 |
+----+---------------------+
3 rows in set (0.00 sec)
mysql>update timetest set id=10 where id=1
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql>select * from timetest
+----+---------------------+
| id | modtime |
+----+---------------------+
| 2 | 2011-09-21 09:56:24 |
| 3 | 2011-09-21 09:56:24 |
| 10 | 2011-09-21 09:57:15 |
+----+---------------------+
3 rows in set (0.00 sec)
mysql>update timetest set id=4 where id=3
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql>select * from timetest
+----+---------------------+
| id | modtime |
+----+---------------------+
| 2 | 2011-09-21 09:56:24 |
| 4 | 2011-09-21 09:58:10 |
| 10 | 2011-09-21 09:57:15 |
+----+---------------------+
3 rows in set (0.00 sec)
mysql>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)