What is the purpose for MySQL's DATE_SUB() function if you can just use DATE_ADD() with a negative interval? Is there a reason for choosing one option over the other?
-- Equivalent:
SELECT DATE_SUB(NOW(), INTERVAL 24 HOUR);
SELECT DATE_ADD(NOW(), INTERVAL -24 HOUR);
You're correct DATE_SUB() and DATE_ADD() do the same thing. As it happens,
SELECT NOW() - INTERVAL 24 HOUR
also does what you show in your example.
There's no measurable efficiency difference between these forms. It's wise to choose the one that makes your query the easiest to understand for the next person to look at it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With