gpt4 book ai didi

Mysql 毫秒插入

转载 作者:行者123 更新时间:2023-11-29 17:14:33 24 4
gpt4 key购买 nike

我们正在为我们的项目构建聊天系统。当用户创建聊天组或向组发送消息时,我们必须以毫秒为单位获取用户事件时间。示例数据库如下。

CREATE TABLE `chats` (
`id` int(11) NOT NULL,
`chat_id` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`status` float NOT NULL DEFAULT '1',
`chat_user` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`user_type` varchar(11) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'users',
`last_seen` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`delete_date` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`update_date` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
`create_date` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

问题是,当我们向表中插入新数据时,mysql将当前时间视为“0000-00-00 00:00:00.000”。全部为零,甚至没有日期。但如果我们在插入后编辑该数据,current_timestamp 就可以完美工作。

最佳答案

来自 MySQL documentation :

When inserting a new row, the default value for a column with an expression default can be inserted either by omitting the column name or by specifying the column as DEFAULT (just as for columns with literal defaults):

因此,您在此处获取默认值的选项是执行根本不提及该列的插入,或者使用占位符DEFAULT。以下两项应生成相同的结果:

INSERT INTO chats (id, chat_id, status, chat_user, user_type, last_seen, delete_date,
update_date, create_date)
VALUES
(1, 1, 1.0, 'tim', 'admin', DEFAULT, DEFAULT, DEFAULT, DEFAULT);

INSERT INTO chats (id, chat_id, status, chat_user, user_type)
VALUES
(1, 1, 1.0, 'tim', 'admin');

关于Mysql 毫秒插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51701839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com