Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 使用当前日期创建数据库_Mysql_Datetime - Fatal编程技术网

Mysql 使用当前日期创建数据库

Mysql 使用当前日期创建数据库,mysql,datetime,Mysql,Datetime,我正在建立一个用于演示软件的数据库。数据库中的日期字段需要是最新的,因为它是一个脚本,将不断重建,因此日期将始终是当前日期的当前日期。但是,DATE()函数似乎工作不正常 例如,我在命令行上将一个mySql脚本导入数据库,这是正在插入的表之一 -- Table structure for table `tasks` -- CREATE TABLE `tasks` ( `id` int(11) unsigned NOT NULL, `funeral_id` int(11) DEFAULT

我正在建立一个用于演示软件的数据库。数据库中的日期字段需要是最新的,因为它是一个脚本,将不断重建,因此日期将始终是当前日期的当前日期。但是,
DATE()
函数似乎工作不正常

例如,我在命令行上将一个mySql脚本导入数据库,这是正在插入的表之一

-- Table structure for table `tasks`
--

CREATE TABLE `tasks` (
  `id` int(11) unsigned NOT NULL,
  `funeral_id` int(11) DEFAULT NULL,
  `deceased_id` int(11) DEFAULT NULL,
  `contact_id` int(11) DEFAULT NULL,
  `task_type_id` int(3) DEFAULT NULL,
  `assigned_id` varchar(30) DEFAULT NULL,
  `start_date` datetime DEFAULT NULL,
  `end_date` datetime DEFAULT NULL,
  `notes` text,
  `courtesy_call` tinyint(1) DEFAULT NULL,
  `q_accepted` tinyint(1) DEFAULT NULL,
  `q_mailed` tinyint(1) DEFAULT NULL,
  `q_returned` tinyint(1) DEFAULT NULL,
  `status_id` int(1) DEFAULT '0',
  `user_created_id` int(3) DEFAULT NULL,
  `created` datetime DEFAULT NULL,
  `user_modified_id` int(3) DEFAULT NULL,
  `modified` datetime DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

--
-- Dumping data for table `tasks`
--

INSERT INTO `tasks` (`id`, `funeral_id`, `deceased_id`, `contact_id`, `task_type_id`, `assigned_id`, `start_date`, `end_date`, `notes`, `courtesy_call`, `q_accepted`, `q_mailed`, `q_returned`, `status_id`, `user_created_id`, `created`, `user_modified_id`, `modified`) VALUES
(1, 1, 1, 1, 1, '39', NULL, 'DATE_ADD(NOW(), INTERVAL 4 DAY)', NULL, 0, NULL, NULL, NULL, 0, 1, '2016-05-27 16:40:17', NULL, '2016-05-27 16:40:17');
我想将
end\u date
字段构建为今天加四天,函数
date\u ADD(NOW(),INTERVAL 4 DAY)
返回
0000-00-00:00:00

另外,我尝试使用
NOW()
CURRENT\u TIMESTAMP()
的任何地方都会返回相同的值
0000-00-00:00:00

为什么会这样?

删除日期中的引号。\u添加(…)

您还可以使用
DATE\u ADD(现在(),间隔4天)
DATE\u ADD(CURDATE(),间隔4天)

您可以参考Mysql文档了解详细信息:

这是一个小错误

您试图插入“DATE_ADD(NOW(),INTERVAL 4 DAY)”,但函数名周围的引号将其转换为字符串,因此它会发出警告并插入0000-00-00

尝试删除引号

INSERT INTO `tasks` (`id`, `funeral_id`, `deceased_id`, `contact_id`, `task_type_id`, `assigned_id`, `start_date`, `end_date`, `notes`, `courtesy_call`, `q_accepted`, `q_mailed`, `q_returned`, `status_id`, `user_created_id`, `created`, `user_modified_id`, `modified`) VALUES
(1, 1, 1, 1, 1, '39', NULL, DATE_ADD(NOW(), INTERVAL 4 DAY), NULL, 0, NULL, NULL, NULL, 0, 1, '2016-05-27 16:40:17', NULL, '2016-05-27 16:40:17');

此外,它还应与CURDATE()配合使用。

它提供相同的result@NishuTayal当前位置你的回答没有纠正错误。看看里面。@RavinderReddy:哦,是的。。查询中的输入错误。纠正它
INSERT INTO `tasks` (`id`, `funeral_id`, `deceased_id`, `contact_id`, `task_type_id`, `assigned_id`, `start_date`, `end_date`, `notes`, `courtesy_call`, `q_accepted`, `q_mailed`, `q_returned`, `status_id`, `user_created_id`, `created`, `user_modified_id`, `modified`) VALUES
(1, 1, 1, 1, 1, '39', NULL, DATE_ADD(NOW(), INTERVAL 4 DAY), NULL, 0, NULL, NULL, NULL, 0, 1, '2016-05-27 16:40:17', NULL, '2016-05-27 16:40:17');