MySQL: Shouldn’t these two DATE queries be the same

mariadbMySQL

I have this query, it tells me what order has expired, immediately after it expires.

(
  SELECT
    *
  FROM
    orders as ord
  WHERE
    DATE_ADD(paid_on, INTERVAL ord.duration HOUR) < NOW()
    AND DATE(DATE_ADD(paid_on, INTERVAL ord.duration HOUR)) BETWEEN DATE(NOW() - INTERVAL 5 DAY)
    AND DATE(NOW())
)
UNION
  (
    SELECT
      *
    FROM
      orders as ord
    WHERE
      DATE_ADD(paid_on, INTERVAL ord.extended_duration HOUR) < NOW()
      AND DATE(
        DATE_ADD(paid_on, INTERVAL ord.extended_duration HOUR)
      ) BETWEEN DATE(NOW() - INTERVAL 5 DAY)
      AND DATE(NOW())
  )

I have decided to optimize it by rewriting it like this

(
  SELECT
    *
  FROM
    orders as ord
  WHERE
    DATE_ADD(ord.paid_on, INTERVAL ord.duration HOUR) < NOW()
    AND DATE_ADD(ord.paid_on, INTERVAL ord.duration HOUR) BETWEEN (CURDATE() - INTERVAL 5 DAY)
    AND CURDATE()
)
UNION
  (
    SELECT
      *
    FROM
      orders as ord
    WHERE
      DATE_ADD(ord.paid_on, INTERVAL ord.extended_duration HOUR) < NOW()
      AND DATE_ADD(ord.paid_on, INTERVAL ord.extended_duration HOUR) BETWEEN (CURDATE() - INTERVAL 5 DAY)
      AND CURDATE()
  )

All I did is using CURDATE() instead of DATE(NOW()), but the queries are retrieving different results, the first query is working correctly, the second query sometimes waits 24h before showing something as expired. DBFiddle

Best Answer

AND  DATE_ADD(ord.paid_on, INTERVAL ord.extended_duration HOUR)

is a DATETIME, which might be in the middle of today.

BETWEEN (CURDATE() - INTERVAL 5 DAY) AND CURDATE()

excludes anything after midnight this morning.

Please provide the order that is miscategorized.