Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the point of MySQL's DATE_SUB() function if negative DATE_ADD() is the same?

Tags:

date

mysql

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);
like image 492
reformed Avatar asked Jan 21 '26 19:01

reformed


1 Answers

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.

like image 101
O. Jones Avatar answered Jan 23 '26 10:01

O. Jones



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!