使用“AND”的简单mysql查询不起作用,但每个方面或变体都可以

时间:2013-01-20 17:01:21

标签: mysql

很难看到这个看似非常简单的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上添加了一个索引,然后输入+ startDT(addedDT有一个)。 没有差别。

startDT和addedDT是DATETIME字段。 type是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"

LEFT SIDE:

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"

SCHEMA:

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');

2 个答案:

答案 0 :(得分:0)

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

select * from events where `type` = 'Disposition' AND `startDT` > DATE('2012-01-01');

答案 1 :(得分:0)

将日期添加到where子句

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

OR

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

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

可在此处找到类似的帖子How to select date from datetime column?