INSERT INTO SQL DATEADD Yesterday

number5 picture number5 · Nov 21, 2012 · Viewed 9.7k times · Source

I want to use PHP and MySQL to INSERT the the day of Yesterday. So my idea was:

INTO chartValues SET timestamp='1353369600', `datetime`=DATEADD(d,-1,GETDATE())

But its not working:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTO chartValues SET timestamp='1353369600', datetime=DATEADD(d,-1,GETDATE())' at line 1

Thanks in advance

Answer

Kermit picture Kermit · Nov 21, 2012

DATEADD and GETDATE() are T-SQL functions used in SQL Server.

You want to use DATE_ADD() or DATE_SUB() with NOW()

INSERT INTO chartValues SET timestamp='1353369600', `datetime`= DATE_SUB(NOW(), INTERVAL 1 DAY)

Reference

DATE_SUB(date, INTERVAL expr unit)