为什么这个 mysql 表在日期时间字段中产生 NULL?

发布于 2024-07-25 18:41:16 字数 691 浏览 0 评论 0原文

这张桌子我有。 该表有一堆字符字段,但相关字段(过期)包含 test_tmp 表上的一个字符和测试表上的一个日期时间。 (为了保护客户端,表名称已更改。)

在 test_tmp 表中,该字段包含月-日期对,例如“Aug 10”或“Feb 20”。 我用来插入数据的代码是:

INSERT IGNORE INTO test (RECNUM, ORDERNUM, CREATEDATE, EXPIREDATE, FNAME, LNAME)
  SELECT RECNUM, ORDERNUM, CREATEDATE,
    CAST(CASE WHEN str_to_date(concat(expiredate, ' ', year(now())), '%b %d %Y') <= CURDATE()
    THEN str_to_date(concat(expiredate, ' ', 1+year(now())), '%b %d %Y')
    ELSE str_to_date(concat(expiredate, ' ', year(now())), '%b %d %Y') END AS DATETIME) AS EXPIREDATE, FNAME, LNAME FROM test_tmp

真正令人困惑的是,如果我运行查询的“SELECT”部分,一切看起来都很好。 但是如果我运行整个过程(首先截断测试表),则每行在过期日期中都包含 NULL。

I've got this table. The table has a bunch of char fields, but the field in question (expiredate) contains a char on the test_tmp table, and a datetime on the test table. (Table names have been changed to protect the clients.)

In the test_tmp table, the field contains a Month-Date pair, like 'Aug 10' or 'Feb 20'. The code I'm using to insert the data is:

INSERT IGNORE INTO test (RECNUM, ORDERNUM, CREATEDATE, EXPIREDATE, FNAME, LNAME)
  SELECT RECNUM, ORDERNUM, CREATEDATE,
    CAST(CASE WHEN str_to_date(concat(expiredate, ' ', year(now())), '%b %d %Y') <= CURDATE()
    THEN str_to_date(concat(expiredate, ' ', 1+year(now())), '%b %d %Y')
    ELSE str_to_date(concat(expiredate, ' ', year(now())), '%b %d %Y') END AS DATETIME) AS EXPIREDATE, FNAME, LNAME FROM test_tmp

What's really puzzing is that if I run the 'SELECT' section of the query everything looks fine. But if I run the whole thing (truncating the test table first), every row contains NULL in expiredate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

め可乐爱微笑 2024-08-01 18:41:16

遗憾的是我还不能设置评论。 这就是为什么我必须把它写在这里。

在 SQL 语句的末尾有一个“)”。 当您复制/粘贴此代码时,是否可能会删除更多内容?

Sadly I can not set comments yet. That's why I have to write it here.

At the end of the SQL statement you have a ')' to much. Is there maybe more cut out while you copy/pasted this code?

束缚m 2024-08-01 18:41:16

您收到任何错误消息吗?

您可以将 IGNORE 保留掉,然后查看是否有任何消息吗?

根据 MySQL 网站
如果未指定 IGNORE,则会触发错误的数据转换会中止该语句。 使用 IGNORE,无效值将调整为最接近的值并插入; 产生警告但语句不会中止。

您的情况可能会发生这样的调整。 这个expiredate到底是一个什么样的字段呢?

Do you get any error messages?

Could you leave the IGNORE away and then see if you have any messages?

According to the MySQL website:
Data conversions that would trigger errors abort the statement if IGNORE is not specified. With IGNORE, invalid values are adjusted to the closest values and inserted; warnings are produced but the statement does not abort.

Such an adjustment could happen in your case. What kind of field is this expiredate exactly?

爱格式化 2024-08-01 18:41:16

为了测试您的查询,我运行了以下命令:

SELECT CAST(CASE WHEN str_to_date(concat('Jan 5', ' ', year(now())), '%b %d %Y') <= CURDATE()
    THEN str_to_date(concat('Jan 5', ' ', 1+year(now())), '%b %d %Y')
    ELSE str_to_date(concat('Jan 5', ' ', year(now())), '%b %d %Y') END AS DATETIME) AS EXPIREDATE;

这工作得很好。 只是为了确保,EXPIREDATE 应该采用“Jan 5”等形式,否则 str_to_date 失败会导致 NULL。

To test your query, I ran the following:

SELECT CAST(CASE WHEN str_to_date(concat('Jan 5', ' ', year(now())), '%b %d %Y') <= CURDATE()
    THEN str_to_date(concat('Jan 5', ' ', 1+year(now())), '%b %d %Y')
    ELSE str_to_date(concat('Jan 5', ' ', year(now())), '%b %d %Y') END AS DATETIME) AS EXPIREDATE;

This worked just fine. Just to make sure, EXPIREDATE should be in the form "Jan 5" etc, or else the NULL results from str_to_date failure.

软甜啾 2024-08-01 18:41:16

好吧,我终于找到了自己问题的答案。

请注意我如何在问题上添加“drupal”标签? 查询正在通过 Drupal...如果您不希望它用于替换数据,则需要转义 % 字符。 因此, str_to_date(concat(expiredate, ' ',year(now())), '%b %d %Y') 实际上需要是 str_to_date(concat(expiredate, ' ') , 年(now())), '%%b %%d %%Y')

谢谢大家的建议。

Well, I just found the answer to my own question, finally.

Notice how I put the 'drupal' tag on the question? The query was going through Drupal... which requires you to escape the % character if you don't want it used to replace data. So, str_to_date(concat(expiredate, ' ', year(now())), '%b %d %Y') actually needed to be str_to_date(concat(expiredate, ' ', year(now())), '%%b %%d %%Y').

Thanks for the suggestions, folks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文