SQL语句和或之间

时间:2013-01-18 22:05:35

标签: sql pdo

我在下面的SQL PDO语句中遇到语法错误,你能告诉我错误在哪里吗? 谢谢!

SELECT * 
FROM events 
WHERE 
    (start_date BETWEEN '2013-01-14' AND '2013-01-20') 
    OR (start_date NOT < '2013-01-14' and end_date > '2013-01-20')

3 个答案:

答案 0 :(得分:4)

你在第一部分的日期之后错过了一个'。

SELECT * FROM events WHERE (start_date BETWEEN '2013-01-14' AND '2013-01-20') OR (start_date NOT < '2013-01-14' and end_date > '2013-01-20')

答案 1 :(得分:2)

你不能把NOT放在表达式的中间,它在表达式之前:

NOT start_date < '2013-01-14'

或者使用<运算符的补码,即>=

start_date >= '2013-01-14'

(使用NOT的例外情况是IS NULL运算符,其中NOT x IS NULLx IS NOT NULL都有效。)

答案 2 :(得分:1)

您在2013-01-20之后错过了一个勾号

SELECT * FROM events 
WHERE 
(start_date BETWEEN '2013-01-14'  AND '2013-01-20')  
OR (start_date >= '2013-01-14' and end_date > '2013-01-20')