MySQL using BETWEEN comparison with NULL

后端 未结 3 1600
难免孤独
难免孤独 2021-01-05 15:58

I have a query with a where condition like so:

WHERE A.event_date BETWEEN B.start_date AND B.end_date

The complexity is that if B.sta

3条回答
  •  青春惊慌失措
    2021-01-05 16:44

    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 query is more complex and not using BETWEEN but finaly I will choose that solution.

提交回复
热议问题