[英]MySQL query to select dates where field is NULL or field date value is not greater than another date
I have this MySQL
query: 我有这个MySQL
查询:
SELECT DISTINCT
*
FROM
students s
LEFT JOIN
(SELECT
*
FROM
studentabsences a1
WHERE
(a1.absence_date = '2013-01-28')) a ON a.absence_student_id = s.student_id
LEFT JOIN
(SELECT
*
FROM
studentabsencenotes b1
WHERE
(b1.absence_note_date = '2013-01-28')
GROUP BY absence_note_student_id) b ON b.absence_note_student_id = s.student_id
INNER JOIN
studentdates sd ON sd.student_id = s.student_id
INNER JOIN
coursecategory ctc ON s.student_course_category_id = ctc.category_id
WHERE
'2013-01-28' BETWEEN sd.student_startdate AND sd.student_enddate
ORDER BY s.student_lastname , s.student_firstname
Now I would like to add another WHERE
clause
to select rows that has not a greater sd.student_break_date
than 2013-01-28 but also select empty fields 现在,我想补充的另一个WHERE
clause
来选择并没有更大的行sd.student_break_date
比2013年1月28日还要选择空字段
For example: 例如:
WHERE '2013-01-28' >= sd.student_break_date
This works but those rows that has NULL
values in the sd.student_break_date
are not listed. 此方法有效,但未列出sd.student_break_date
中具有NULL
值的那些行。
Messy, huh? 凌乱吧? My question is: How can I select fields that are empty AND fields where date is not higher than 2013-01-28 我的问题是:如何选择空白字段以及日期不高于2013-01-28的字段
Try this :: 尝试这个 ::
WHERE sd.student_break_date is null OR '2013-01-28' >= sd.student_break_date
OR you can try :: 或者您可以尝试::
WHERE sd.student_break_date is null OR sd.student_break_date<'2013-01-28'
OR :: 要么 ::
WHERE IFNULL(sd.student_break_date,'2013-01-27') <'2013-01-28'
BETWEEN is an odd one for this bit of code 对于这段代码,BETWEEN是一个奇怪的代码
'2013-01-28' BETWEEN sd.student_startdate AND sd.student_enddate
You're actually doing sd.student_startdate>'2013-01-28' AND sd.student_enddate<'2013-01-28'. 您实际上是在做sd.student_startdate>'2013-01-28'和sd.student_enddate <'2013-01-28'。 Notice there's no = in there so its actually excluding the 28th in both cases. 注意那里没有=,所以在两种情况下它实际上都排除了第28位。 If you want to actually include the 28th in your clause then a BETWEEN is not really any use. 如果您想在子句中实际包含28号,那么BETWEEN并不是真正的用处。
As for you're question assuming the badly worded question means you want an sd.studen_break_date which is either null or less than the 28th then it'd be 至于您的问题,假设措辞不佳意味着您想要一个sd.studen_break_date,该值可以为null或小于28,那么它将是
WHERE sd.student_startdate>'2013-01-28' AND sd.student_enddate<'2013-01-28' AND (sd.student_break_date is null OR sd.student_break_date<='2013-01-28')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.