mysql返回值,即使它为null

时间:2014-08-05 10:00:25

标签: mysql

如果日期范围匹配,我有一个返回值的查询。如果它不匹配则不返回任何内容。

SELECT start, end
FROM (`taxemployee`) 
LEFT JOIN `project_staff_assignment` ON `project_staff_assignment`.`taxemployee_id` = `taxemployee`.`id` 

 WHERE (start >= '2014-09-01' AND end <= '2014-09-15')
    OR (end >=  '2014-09-15' AND start <= '2014-09-01')

GROUP BY `taxemployee`.`id` 
ORDER BY `assigned_hours`ASC

如果我把它放在WHERE

OR coalesce(`start`, `end`) IS NULL

它返回空值,但不包含值

如果结果为null且值为

,我该如何返回结果

这里是sqlfiddle

http://sqlfiddle.com/#!2/8bbe4/3

1 个答案:

答案 0 :(得分:0)

<强>更新

请参阅此处的小提琴:http://sqlfiddle.com/#!2/2babf/2

我正在使用case语句显示日期,如果它在范围内,如果没有则显示null。

SELECT `taxemployee`.`id`, Nickname, 
case when project_staff_assignment.startdt >= '2014-09-01' THEN project_staff_assignment.STARTdt ELSE NULL  END AS STARTDATE,
case when project_staff_assignment.ENDdt >= '2014-09-15' THEN project_staff_assignment.ENDdt ELSE NULL END AS ENDDATE
FROM (`taxemployee`) 
LEFT JOIN `project_staff_assignment` ON `project_staff_assignment`.`taxemployee_id` = `taxemployee`.`id` 

GROUP BY `taxemployee`.`id` 
ORDER BY `taxemployee`.`id`  ASC

原始答案:

请参阅此处的小提琴:http://sqlfiddle.com/#!2/8bbe4/52

SELECT `taxemployee`.`id`, Nickname, start, end
FROM (`taxemployee`) 
LEFT JOIN `project_staff_assignment` ON `project_staff_assignment`.`taxemployee_id` = `taxemployee`.`id` 

 WHERE (start >= '2014-08-01' AND end <= '2014-08-15')
    OR (end >=  '2014-08-15' AND start <= '2014-09-01')
    OR coalesce(`start`, `end`) IS NULL

GROUP BY `taxemployee`.`id` 
ORDER BY `taxemployee`.`id`  ASC;

未返回记录,因为, 价值仅适用于01.08.2014,而您的条件是01.09.2014