为什么这个 mysql 表在日期时间字段中产生 NULL?
这张桌子我有。 该表有一堆字符字段,但相关字段(过期)包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
遗憾的是我还不能设置评论。 这就是为什么我必须把它写在这里。
在 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?
您收到任何错误消息吗?
您可以将 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?
为了测试您的查询,我运行了以下命令:
这工作得很好。 只是为了确保,EXPIREDATE 应该采用“Jan 5”等形式,否则 str_to_date 失败会导致 NULL。
To test your query, I ran the following:
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.
好吧,我终于找到了自己问题的答案。
请注意我如何在问题上添加“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 bestr_to_date(concat(expiredate, ' ', year(now())), '%%b %%d %%Y')
.Thanks for the suggestions, folks.