MySQL - 只有在不是0时才抓住TIME(00:00:00)

时间:2010-11-17 07:36:54

标签: mysql

这是我正在使用的SELECT(不起作用)

SELECT 
    IF( 
         TIME( `date_time` ) = '00:00:00', 
         DATE( `date_time` ), 
         `date_time`
    ) AS date_time
FROM `table`

我的表:

CREATE TABLE `graphs` (
  `graph_id` int(11) NOT NULL auto_increment,
  `company_id` int(11) NOT NULL,
  `stat_book_id` int(11) NOT NULL,
  `object_id` int(11) NOT NULL,
  `object_type` varchar(50) NOT NULL,
  `created_user_id` int(11) NOT NULL,
  `name` varchar(50) NOT NULL,
  `status` tinyint(1) NOT NULL default '1',
  `sparkline` varchar(255) NOT NULL,
  `sparkline_updated` datetime NOT NULL,
  `date_created` datetime NOT NULL,
  `date_updated` timestamp NOT NULL default '0000-00-00 00:00:00' on update CURRENT_TIMESTAMP,
  PRIMARY KEY  (`graph_id`),
  KEY `stat_book_id` (`stat_book_id`,`object_id`,`object_type`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

版本数据

Server version: 5.0.91-community
time format: %H:%i:%s

此查询:

SELECT 
    `date_time`, DATE(`date_time`), TIME(`date_time`) 
FROM `graph_values` 
LIMIT 10

返回:

date_time   DATE(`date_time`)   TIME(`date_time`)
2010-09-24 00:00:00     2010-09-24  00:00:00
2010-09-19 00:00:00     2010-09-19  00:00:00
2010-09-16 21:05:00     2010-09-16  21:05:00
2010-10-30 00:00:00     2010-10-30  00:00:00
2010-10-08 00:00:00     2010-10-08  00:00:00
2010-09-17 23:50:00     2010-09-17  23:50:00
2010-09-15 00:00:00     2010-09-15  00:00:00
2010-10-23 00:00:00     2010-10-23  00:00:00
2010-09-17 19:24:00     2010-09-17  19:24:00
2010-09-19 19:24:00     2010-09-19  19:24:00

但是,此查询会返回不正确的结果:

SELECT 
    `date_time`, DATE(`date_time`), TIME(`date_time`), IF( 
        TIME( `date_time` ) = '00:00:00', 
        DATE( `date_time` ), 
        `date_time`
    )
FROM `graph_values`
LIMIT 10

结果:

date_time   DATE(`date_time`)   TIME(`date_time`)   IF( TIME( `date_time` ) = '00:00:00', DATE( `date_time` ), `date_time` )
2010-09-24 00:00:00     2010-09-24  00:00:00    2010-09-24 00:00:00
2010-09-19 00:00:00     2010-09-19  00:00:00    2010-09-19 00:00:00
2010-09-16 21:05:00     2010-09-16  21:05:00    2010-09-16 21:05:00
2010-10-30 00:00:00     2010-10-30  00:00:00    2010-10-30 00:00:00
2010-10-08 00:00:00     2010-10-08  00:00:00    2010-10-08 00:00:00
2010-09-17 23:50:00     2010-09-17  23:50:00    2010-09-17 23:50:00
2010-09-15 00:00:00     2010-09-15  00:00:00    2010-09-15 00:00:00
2010-10-23 00:00:00     2010-10-23  00:00:00    2010-10-23 00:00:00
2010-09-17 19:24:00     2010-09-17  19:24:00    2010-09-17 19:24:00
2010-09-19 19:24:00     2010-09-19  19:24:00    2010-09-19 19:24:00

任何人都知道为什么?


更新:找到解决方案。

似乎有一个错误或它应该是上述方式。似乎问题是它不能以两种不同的方式返回date格式(一种是时间,一种是没有)。似乎旧版本的MySQL可以(参见下面的测试)。

在SQL数据库中进行了相同的测试,它也遇到了同样的问题。简单的解决方案如下(我相信它具有可比较的执行时间):

SELECT 
     IF( 
          TIME( `date_time` ) = '00:00:00', 
          REPLACE( `date_time`, ' 00:00:00', '' ), 
          `date_time`
     ) AS date_time
FROM `table`

1 个答案:

答案 0 :(得分:2)

我在想你是这样做的意思:

SELECT 
     IF( 
          TIME( `date_time` ) = '00:00:00', 
          DATE( `date_time` ), 
          `date_time` 
     ) AS date_time
 FROM `table`

那样

2010-11-17 08:43:00 -> 2010-11-17 08:43:00
2010-11-17 00:00:00 -> 2010-11-17

成熟的例子:

CREATE TABLE `testdates` ( `date_time` datetime NOT NULL);

insert into testdates values (now());
insert into testdates values ("2010-11-17 00:00:05");
insert into testdates values ("2010-11-16 00:00:00");
insert into testdates values ("2010-11-15");

SELECT 
     IF( 
          TIME( `date_time` ) = '00:00:00', 
          DATE( `date_time` ), 
          `date_time` 
     ) AS date_time
 FROM testdates;

+---------------------+
| date_time           |
+---------------------+
| 2010-11-17 08:52:06 |
| 2010-11-17 00:00:05 |
| 2010-11-16          |
| 2010-11-15          |
+---------------------+

我的配置:

mysql> show variables like "version";
+---------------+------------+
| Variable_name | Value      |
+---------------+------------+
| version       | 5.0.26-log |
+---------------+------------+
1 row in set (0.00 sec)

mysql> show variables like "time_format";
+---------------+----------+
| Variable_name | Value    |
+---------------+----------+
| time_format   | %H:%i:%s |
+---------------+----------+
1 row in set (0.00 sec)