我有下表:
+-------------+------------------+------+-----+-------------------+----------------+
| 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 |
我确信在运行查询时数据完好无损。
所以我的问题:
任何人都可以解释为什么日期函数在同一输入上显示不同的结果吗?
答案 0 :(得分:3)
你必须使用::
select DATE(created_on) from categories limit 10;
通过使用反转的逗号,您要求数据库将'created_on'
视为参数,并且因为它无法转换为日期类型,所以您将变为空
答案 1 :(得分:3)
select DATE('created_on') from categories limit 10;
字符串与列:
select DATE(created_on) from categories limit 10;