-2

这个看似非常简单的 mysql 查询很难处理:

这不起作用(有效,但不返回任何行):

select * from events where type = 'Disposition' AND startDT > '2012-01-01'

以下所有查询都使用相同的数据:

select * from events where type = 'Eval' AND startDT > '2012-01-01'    
-- (change the value to Eval it works!)

select * from events where type = 'Disposition' AND addedDT > '2012-01-01' 
-- (search a different date field it works!)

select * from events where type = 'Disposition'  
-- (get rid of the AND it works!)

select * from events where startDT > '2012-01-01'   
-- (again, no AND it works!)

我尝试了错误查询的变体,但也不起作用:

select * from events where `type` = 'Disposition' AND `startDT` > '2012-01-01'
select * from events where `type` = 'Disposition' AND `startDT` > '2012-01-01 00:00:00'
select events.* from events where `type` = 'Disposition' 
    AND `startDT` > '2012-01-01 00:00:00'

(还尝试了日期的变化,小写处置)

我在这些字段上没有索引,所以我在 startDT 上添加了一个索引,然后 type+startDT(addDT 有一个)。没有不同。

startDT 和 addedDT 是 DATETIME 字段。类型是 varchar 30

有任何想法吗?

-- 如果这有帮助,这里是解释输出:

坏的一个:

explain select * FROM `events` WHERE `startDT` > '2012-01-11 00:00:01' AND `type` = "Disposition" limit 1000
"id"    "select_type"   "table" "type"  "possible_keys" "key"   "key_len"   "ref"   "rows"  "Extra"
"1" "SIMPLE"    "events"    "range" "startDT,type"  "type"  "40"    NULL    "1" "Using where"

右边:

explain select * FROM `events` WHERE `type` = "Disposition" limit 1000
"id"    "select_type"   "table" "type"  "possible_keys" "key"   "key_len"   "ref"   "rows"  "Extra"
"1" "SIMPLE"    "events"    "ref"   "type"  "type"  "32"    "const" "13760" "Using where"

左边:

explain select * FROM `events` WHERE `startDT` > '2012-01-11 00:00:01' limit 1000
"id"    "select_type"   "table" "type"  "possible_keys" "key"   "key_len"   "ref"   "rows"  "Extra"
"1" "SIMPLE"    "events"    "range" "startDT"   "startDT"   "8" NULL    "13760" "Using where"

架构:

CREATE TABLE `events` (
    `id` VARCHAR(80) NOT NULL,
    `pk` INT(11) NOT NULL AUTO_INCREMENT,
    `addedDT` DATETIME NOT NULL,
    `type` VARCHAR(30) NOT NULL,
    `startDT` DATETIME NOT NULL,
    PRIMARY KEY (`pk`),
    INDEX `addedDT` (`addedDT`),
    INDEX `startDT` (`startDT`),
    INDEX `type` (`type`, `startDT`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=29407;

示例行:

INSERT INTO `events` (`id`, `pk`, `addedDT`, `type`, `startDT`) VALUES ('FBF8CE96-AE86-36C6-3C27-E2AAD042476B', 29406, '2012-12-28 12:59:03', 'Disposition', '2012-12-28 12:59:00');
INSERT INTO `events` (`id`, `pk`, `addedDT`, `type`, `startDT`) VALUES ('12577DB6-9B22-FA73-89BD-E2A5C1764B6D', 29405, '2012-12-28 12:53:32', 'Disposition', '2012-12-28 12:53:00');
4

2 回答 2

0

startDT是表中的日期类型,请尝试将日期比较为-

select * from events where `type` = 'Disposition' AND `startDT` > DATE('2012-01-01');
于 2013-01-20T17:11:14.590 回答
0

在 where 子句中添加日期

WHERE `startDT` >  DATE('2012-01-01')

或者

WHERE DATE(`startDT`) >  ('2012-01-01')

http://www.sqlfiddle.com/#!2/4c879/7

可以在此处找到类似的帖子如何从日期时间列中选择日期?

于 2013-01-20T17:28:10.153 回答