简体   繁体   English

MySQL,如何插入null日期

[英]MySQL, how to insert null dates

I am having trouble inserting null values into date fields into a MySQL table.我无法将 null 值插入日期字段到 MySQL 表中。

Here is the insert query:这是插入查询:

$query = 'INSERT INTO table (column_s1, column_s2, column_d1, column_d2)
VALUES ("'.$string1.'", "'.$string2.'", '.$date1.', '.$date2.')';

Columns s1 and s2 take string values and d1 and d2 take dates. s1 和 s2 列采用字符串值,d1 和 d2 采用日期。 When I run this query with only the string fields, there is no problem.当我仅使用字符串字段运行此查询时,没有问题。

The date values can be either set or null, so I have not included the quotation marks in the query, but have instead added them to the variable earlier on.日期值可以设置为 null,因此我没有在查询中包含引号,而是在前面将它们添加到变量中。 This is the php code I am using to set the date values:这是我用来设置日期值的 php 代码:

if (empty($date1)){
    $date1 = NULL;
}
else{
    $date1part = explode("/",$date1);
    $date1 = '"'.$date1part[2].'/'.$date1part[1].'/'.$date1part[0].'"';
}

When the date values are all set, the record is inserted correctly.当所有日期值都设置好后,记录就被正确插入了。 However, when either of the dates is null, nothing is inserted.但是,当任一日期为 null 时,不会插入任何内容。

Why can't I just insert null values into MySQL like this?为什么我不能像这样将 null 值插入 MySQL?

Try this:尝试这个:

$query = "INSERT INTO table (column_s1, column_s2, column_d1, column_d2) 
          VALUES ('$string1', '$string2', " . ($date1==NULL ? "NULL" : "'$date1'") . ", " . ($date2==NULL ? "NULL" : "'$date2'") . ");";

so for example if you put this into query:例如,如果您将其放入查询中:

$string1 = "s1";
$string2 = "s2";
$date1 = NULL;
$date2 = NULL;

result should be:结果应该是:

INSERT INTO table (column_s1, column_s2, column_d1, column_d2) VALUES ('s1', 's2', NULL, NULL);

You should convert the null variable into a NULL string first Like this:您应该先将空变量转换为空字符串,如下所示:

if(is_null($date1)){
    $date1 = 'NULL';
}

If you are using a MySQL date column, you must also specify that it should hold null when creating it, like this:如果您使用的是 MySQL 日期列,则还必须指定它在创建时应为 null,如下所示:

CREATE TABLE `table` (
id INT NOT NULL AUTO_INCREMENT,
date DATE NULL DEFAULT NULL,
PRIMARY KEY(id)
)

It is also very important that you perform the query with bound parameters, for example using pdo使用绑定参数执行查询也非常重要,例如使用 pdo

  1. http://www.php.net/manual/en/pdo.construct.php http://www.php.net/manual/en/pdo.construct.php
  2. http://php.net/manual/en/pdo.prepared-statements.php http://php.net/manual/en/pdo.prepared-statements.php
  3. How do I insert NULL values using PDO? 如何使用 PDO 插入 NULL 值?

Something like this:像这样的东西:

$query = 'INSERT INTO table (column_s1, column_s2, column_d1, column_d2)
VALUES (?, ?, ?, ?)';
$stmt = $db->prepare($query);
$stmt->execute(array($string1,$string2,$date1,$date2));

Backslash N is another way to express NULL in MySQL.反斜杠 N 是在 MySQL 中表示 NULL 的另一种方式。

Try putting the value (backslash N): \\N into one of the parameters like this:尝试将值 (反斜杠 N): \\N放入如下参数之一:

$data1 = "\N";
$sql="insert into tablename set column_s1='" . $data1 . 
  "', column_s2='" . data2 . 
  "', column_s3='" . $data3 . "'";

Reference: http://dev.mysql.com/doc/refman/5.1/en/load-data.html参考: http : //dev.mysql.com/doc/refman/5.1/en/load-data.html

如果 NULL 不起作用,只需将您的日期传递为“0000-00-00”:

$chequeDate = "0000-00-00";

在 Derby 中,如果要插入除已声明为 Null (column_d1, column_d2) 之外的值,sql:

INSERT INTO DB.table (column_s1, column_s2) VALUES ('s1', 's2');

Probably answer is unneeded at this moment, but I found solution exactly I have been searching.目前可能不需要答案,但我找到了我一直在寻找的解决方案。 Use an Expression to pass NULL like this:使用Expression传递NULL如下所示:

['some_date_to_update' => new Expression('NULL')]

Hence, MySQL will understand what you want, and save (NULL) in DB instead of storing 0-dates.因此,MySQL 会理解您想要什么,并在 DB 中保存(NULL)而不是存储 0 日期。 Hope this will help somebody.希望这会帮助某人。

In Mysql DATE data type Default NULL means在 Mysql DATE数据类型中默认NULL表示

Some version set as 0000-00-00某些版本设置为0000-00-00

Some version set as 1970-01-01某些版本设置为1970-01-01

Years later, if someone is still experiencing this issue, you want to use PDO and bind the variables, everything will be taken care of no need to handle the null variables yourself.多年后,如果有人仍然遇到这个问题,你想使用 PDO 并绑定变量,一切都会被处理,而不需要自己处理 null 变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM