MySQL using BETWEEN comparison with NULL

狂风中的少年 提交于 2019-12-04 08:01:09

IFNULL might help out here:

WHERE A.event_date BETWEEN IFNULL(B.start_date,"1900-01-01") 
                   AND IFNULL(B.end_date,now());

The COALESCE() function will return the first non-null value in the parameter list. Give this a try:

WHERE A.event_date BETWEEN COALESCE(B.start_date,'1900-01-01') and COALESCE(B.end_date,CURRENT_TIMESTAMP)

IF PERFORMANCE MATTERS...

When I saw the @tom-mac answer I said : nice solution, I need to test it, and after I saw the @mw-goodjava answer and I wanted to compare.

According to this test http://blogs.x2line.com/al/archive/2004/03/01/189.aspx, it seems to be better to use IFNULL() instead of COALESCE() function but the best is still to use WHERE ... OR ... IS NULL like this :

WHERE (A.event_date >= B.start_date OR B.start_date IS NULL)
  AND (A.event_date <= B.end_date OR B.end_date IS NULL)

So the quesry is more complex and not usint BETWEEN but finaly I will choose that solution.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!