Is there any difference between DATE_SUB() and using arithmetic operators for datetime calculation?

后端 未结 2 1639
小鲜肉
小鲜肉 2021-02-13 12:07

After I have seen a lot of questions here using the DATE_SUB() or DATE_ADD() functions instead of the arithmetic operators + or -

2条回答
  •  暖寄归人
    2021-02-13 12:31

    The MySQL documentation for DATE_ADD (http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add) explicitly states that you may do date arithmetic with the + and - operators.

    Date arithmetic also can be performed using INTERVAL together with the + or - operator:

    date + INTERVAL expr unit

    date - INTERVAL expr unit

    Given that it's endorsed by the docs, I think any difference is stylistic. I personally think the +/- is easier to read (after all, you don't use INT_ADD(...) or DOUBLE_ADD(...) to manipulate numeric values, so why dates?). Others might have their own reasons for liking DATE_ADD and DATE_SUB, and that's fine too. Just pick something and stick with it.

    Jemiah

提交回复
热议问题