0

我有这个又长又讨厌的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 |

4

2 回答 2

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
于 2012-11-05T13:06:19.303 回答
1

ANDOR用于连接布尔表达式。在您的情况下,您有以下表达式:

'2012-10-01' OR '2012-10-31' BETWEEN sd.student_startdate AND sd.student_enddate

这有两个部分(由 OR 关系连接):

  1. '2012-10-01'
  2. '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'
于 2012-11-05T13:06:37.740 回答