Rob*_*oya 17 mysql sql select date
我有以下查询:
SELECT * FROM `Contacts`
WHERE `Zona` = '1'
AND `Responsable` = '9'
AND `AllowComercialVisit` = 'Call_Again'
-- the problem are OR's --
OR `AllowComercialVisit` = 'Busy'
OR `AllowComercialVisit` = 'Not_answered'
-- the problem are OR's --
AND `DateRecall` <= '2016-06-20 13:04:52'
AND `DateRecall` >= '2016-06-20 12:39:52'
ORDER BY `DateRecall` ASC LIMIT 1
Run Code Online (Sandbox Code Playgroud)
问题是查询应该只显示第一个和第二个'DateRecall'之间的行,但是返回所有带有'Call_Again','Busy'和'Not_answered'的行而不过滤日期.
任何解决方案将不胜感激!
sag*_*agi 47
一个简单的IN()
解决方案:
SELECT * FROM `Contacts`
WHERE `Zona` = '1'
AND `Responsable` = '9'
AND `AllowComercialVisit` IN ('Call_Again','Busy','Not_answered')
AND `DateRecall` BETWEEN '2016-06-20 12:39:52'
AND '2016-06-20 13:04:52'
ORDER BY `DateRecall` ASC
LIMIT 1
Run Code Online (Sandbox Code Playgroud)
一般情况下,使用圆括号时使用AND
优先级- >OR
OR
WHERE COND1 AND COND2 AND (COND3 OR COND4) AND COND5
Run Code Online (Sandbox Code Playgroud)
这将强制优化器遵循您的优先级而不是默认优先级.
小智 12
请尝试以下查询:
SELECT * FROM `Contacts`
WHERE `Zona` = '1'
AND `Responsable` = '9'
AND (`AllowComercialVisit` = 'Call_Again' OR `AllowComercialVisit` = 'Busy' OR AllowComercialVisit` = 'Not_answered')
AND `DateRecall` <= '2016-06-20 13:04:52'
AND `DateRecall` >= '2016-06-20 12:39:52'
ORDER BY `DateRecall` ASC
LIMIT 1
Run Code Online (Sandbox Code Playgroud)
用括号括起OR.
尝试将OR语句分组,因为它们与同一列相关,即
SELECT * FROM `Contacts`
WHERE `Zona` = '1'
AND `Responsable` = '9'
AND (`AllowComercialVisit` = 'Call_Again'
OR `AllowComercialVisit` = 'Busy'
OR `AllowComercialVisit` = 'Not_answered' )
AND `DateRecall` <= '2016-06-20 13:04:52'
AND `DateRecall` >= '2016-06-20 12:39:52'
ORDER BY `DateRecall` ASC LIMIT 1
Run Code Online (Sandbox Code Playgroud)