mysql中自动插入时间的格式。 我使用mysql 数据库中设置,当有一条数据插入的时候,会自动插入当前时间…

mysql中自动插入时间的格式。 我使用mysql 数据库中设置,当有一条数据插入的时候,会自动插入当前时间…,第1张

mysql>SELECT

-> 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>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11611211.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存