我有这个冗长而讨厌的MySQL
查询:
SELECT * FROM
studentdates sd
INNER JOIN
students s ON s.student_id = sd.student_id
INNER JOIN
cityselections cs ON cs.city_id = s.student_city_id
INNER JOIN
customers c ON c.customer_id = s.student_customer_id
INNER JOIN
price_to_course_number ptcn ON ptcn.ptcn_course_number = sd.student_course_number
INNER JOIN
pricegroups pg ON pg.price_id = ptcn.ptcn_price_id
WHERE
('2012-10-01' OR '2012-10-31' BETWEEN sd.student_startdate AND sd.student_enddate)
AND cs.city_id = '12'
AND sd.student_course_number = '46248'
ORDER BY s.student_lastname ASC
这将打印以下行(编辑出不必要的列):
+------------+--------------------+-----------------+
| student_id | student_startdate | student_enddate |
+------------+--------------------+-----------------+
| 299 | 2012-09-24 | 2012-10-21 |
| 299 | 2012-09-17 | 2012-09-23 |
+------------+--------------------+-----------------+
请注意,在我的查询中,我有WHERE ('2012-10-01' OR '2012-10-31' BETWEEN sd.student_startdate AND sd.student_enddate)
为什么我得到这个?
| 299 | 2012-09-17 | 2012-09-23 |
答案 0 :(得分:1)
SELECT * FROM
studentdates sd
INNER JOIN
students s ON s.student_id = sd.student_id
INNER JOIN
cityselections cs ON cs.city_id = s.student_city_id
INNER JOIN
customers c ON c.customer_id = s.student_customer_id
INNER JOIN
price_to_course_number ptcn ON ptcn.ptcn_course_number = sd.student_course_number
INNER JOIN
pricegroups pg ON pg.price_id = ptcn.ptcn_price_id
WHERE
sd.student_startdate >='2012-10-01' and sd.student_enddate <='2012-10-31'
AND cs.city_id = '12'
AND sd.student_course_number = '46248'
ORDER BY s.student_lastname ASC
答案 1 :(得分:1)
AND
和OR
用于连接布尔表达式。在您的情况下,您有以下表达式:
'2012-10-01' OR '2012-10-31' BETWEEN sd.student_startdate AND sd.student_enddate
这有两个部分(由OR关系连接):
'2012-10-01'
'2012-10-31' BETWEEN sd.student_startdate AND sd.student_enddate
第一部分将始终评估为true
(它为非零,非空),因此该表达式的结果也将始终为真。
你或许是指这个吗?
sd.student_enddate >= '2012-10-01' AND sd.student_startdate <= '2012-10-31'