1

我有下表:

+-------------+------------------+------+-----+-------------------+----------------+
| Field       | Type             | Null | Key | Default           | Extra          |
+-------------+------------------+------+-----+-------------------+----------------+
| category_id | int(11) unsigned | NO   | PRI | NULL              | auto_increment |
| title       | varchar(64)      | NO   |     | NULL              |                |
| description | text             | NO   |     | NULL              |                |
| created_on  | timestamp        | NO   |     | CURRENT_TIMESTAMP |                |
| updated_on  | timestamp        | YES  |     | NULL              |                |
+-------------+------------------+------+-----+-------------------+----------------+

我需要根据创建日期删除一些记录并执行一组查询:

mysql> select created_on from categories limit 10;
+---------------------+
| created_on          |
+---------------------+
| 2013-01-14 09:26:21 |
| 2012-08-15 11:18:38 |
| 2012-09-06 06:44:46 |
| 2012-09-06 23:27:14 |

然后

mysql> select date('2013-01-14 09:26:21');
+-----------------------------+
| date('2013-01-14 09:26:21') |
+-----------------------------+
| 2013-01-14                  |
+-----------------------------+

最后我得到了:

mysql> select DATE('created_on') from categories limit 10;
+--------------------+
| DATE('created_on') |
+--------------------+
| NULL               |
| NULL               |
| NULL               |
| NULL               |

如您所见,日期未正确转换。在大约 10 分钟内工作正常:

mysql> select date(created_on) from categories limit 10;
+------------------+
| date(created_on) |
+------------------+
| 2013-01-14       |
| 2012-08-15       |
| 2012-09-06       |
| 2012-09-06       |

我确信在运行查询时数据是完整的。

所以我的问题:
谁能解释为什么日期函数在相同的输入上显示不同的结果?

4

2 回答 2

3

你必须使用::

select DATE(created_on) from categories limit 10;

通过使用引号,您要求数据库将'created_on'其视为参数,并且由于它无法转换为日期类型,因此您将获得 null

于 2013-01-21T19:04:02.940 回答
3
select DATE('created_on') from categories limit 10;

字符串与列:

select DATE(created_on) from categories limit 10;
于 2013-01-21T19:05:06.090 回答