条件不工作的地方
Where condition is not working
我有一个 SQL 查询。但这包含一个 where 条件,并且 where 中的 OR 不起作用。
查询
SELECT `st_student`.`st_id`, `ab_date`, `as_date` `st_status`
FROM (`st_student`)
WHERE `st_status` = 1
OR `st_status` = 2
AND `ab_date` BETWEEN '08/01/2015' AND '08/31/2015'
OR `as_date` BETWEEN '08/01/2015' AND '08/31/2015'
AND `aca_no` = 2
GROUP BY `st_student`.`st_id`
这个条件不工作:
OR `as_date` BETWEEN '08/01/2015' AND '08/31/2015'
有没有错?
一个可能的错误涉及 AND
和 OR
运算符的使用,因为 AND
优先于 OR
,并且您的查询可能会被错误地解释方法。所以你应该使用括号,写这样的东西:
SELECT `st_student`.`st_id`, `ab_date`, `as_date` `st_status`
FROM `st_student`
WHERE (`st_status` = 1 OR `st_status` = 2)
AND (`ab_date` BETWEEN '08/01/2015' AND '08/31/2015'
OR `as_date` BETWEEN '08/01/2015' AND '08/31/2015')
AND `aca_no` = 2
GROUP BY `st_student`.`st_id`
如果您的列声明为 varchar 数据类型,您将需要使用 str_to_date
函数。 Varchar 不能作为日期进行比较,除非您将其转换为日期。试试这个,让我知道。祝你好运。
SELECT `st_student`.`st_id`, `ab_date`, `as_date` `st_status`
FROM `st_student`
WHERE (`st_status` = 1 OR `st_status` = 2)
AND STR_TO_DATE(`ab_date`,'%d/%m/%Y')
BETWEEN
STR_TO_DATE('01/08/2015','%d/%m/%Y')
AND
STR_TO_DATE('31/08/2015','%d/%m/%Y')
OR
STR_TO_DATE(`as_date`,'%d/%m/%Y')
BETWEEN
STR_TO_DATE('01/08/2015','%d/%m/%Y')
AND
STR_TO_DATE('31/08/2015','%d/%m/%Y')
AND `aca_no` = 2
GROUP BY `st_student`.`st_id`
我有一个 SQL 查询。但这包含一个 where 条件,并且 where 中的 OR 不起作用。
查询
SELECT `st_student`.`st_id`, `ab_date`, `as_date` `st_status`
FROM (`st_student`)
WHERE `st_status` = 1
OR `st_status` = 2
AND `ab_date` BETWEEN '08/01/2015' AND '08/31/2015'
OR `as_date` BETWEEN '08/01/2015' AND '08/31/2015'
AND `aca_no` = 2
GROUP BY `st_student`.`st_id`
这个条件不工作:
OR `as_date` BETWEEN '08/01/2015' AND '08/31/2015'
有没有错?
一个可能的错误涉及 AND
和 OR
运算符的使用,因为 AND
优先于 OR
,并且您的查询可能会被错误地解释方法。所以你应该使用括号,写这样的东西:
SELECT `st_student`.`st_id`, `ab_date`, `as_date` `st_status`
FROM `st_student`
WHERE (`st_status` = 1 OR `st_status` = 2)
AND (`ab_date` BETWEEN '08/01/2015' AND '08/31/2015'
OR `as_date` BETWEEN '08/01/2015' AND '08/31/2015')
AND `aca_no` = 2
GROUP BY `st_student`.`st_id`
如果您的列声明为 varchar 数据类型,您将需要使用 str_to_date
函数。 Varchar 不能作为日期进行比较,除非您将其转换为日期。试试这个,让我知道。祝你好运。
SELECT `st_student`.`st_id`, `ab_date`, `as_date` `st_status`
FROM `st_student`
WHERE (`st_status` = 1 OR `st_status` = 2)
AND STR_TO_DATE(`ab_date`,'%d/%m/%Y')
BETWEEN
STR_TO_DATE('01/08/2015','%d/%m/%Y')
AND
STR_TO_DATE('31/08/2015','%d/%m/%Y')
OR
STR_TO_DATE(`as_date`,'%d/%m/%Y')
BETWEEN
STR_TO_DATE('01/08/2015','%d/%m/%Y')
AND
STR_TO_DATE('31/08/2015','%d/%m/%Y')
AND `aca_no` = 2
GROUP BY `st_student`.`st_id`