views:

1362

answers:

3
mysql -u username -p database -e deletedata.sql

I get ERROR 1064 (42000) at line 1: 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 'deletedata.sql' at line 1

DELETE FROM 'table1' WHERE 'column' <= date_sub(current_date, INTERVAL 37 day);
DELETE FROM 'table2' WHERE 'column' <= date_sub(current_date, INTERVAL 37 day);

What could I be missing?

+1  A: 

Single quotes in MySQL denote string constants. You should be using back quotes (`) to escape table / column names (or identifiers in general).

ChssPly76
This didn't work...
cypherus
What specifically didn't work? `DELETE FROM \`table1\` WHERE \`column\` <= date_sub(current_date, INTERVAL 37 day);` should work just fine. Are you getting the same error as above?
ChssPly76
Yes, It's the same error as above.
cypherus
A: 

I imagine you're probably getting into trouble with using table identifiers in the FROM clause. I stole some examples from one of my databases. So this will fail:

DELETE FROM Session s WHERE s.startTime<=date_sub(NOW(),INTERVAL 30 day);

Which is really strange because just replacing "DELETE" with "SELECT *" works.

But this will work fine:

DELETE FROM Session WHERE startTime<=date_sub(NOW(),INTERVAL 30 day);
DELETE s FROM Session s WHERE s.startTime<=date_sub(NOW(),INTERVAL 30 day);

Syntactic oddities. Fun! :-D

Brent Nash
This didn't work...
cypherus
A: 

-e executes a statement, not a sql file. What you need:

mysql -u user -p pass db

and then into the mysql cli:

\. deletedata.sql

Backslash dot means "source file"

Or you can just pipe it:

cat delete.sql | mysql -u user -p pass db
WishCow
Thanks...the last think you said to try worked so I went back and looked at my syntax and checked the manual for mysql. This worked for me:mysql --user=user_name --password=your_password db_name > deletesql.sqlNow one last question...there is no output...is there any way to make this verbose so I can get a confirmation that it deleted my data? Thanks.
cypherus
Nevermind...I guess I should have RTFM first before posting. --verbose is the flag to be added to the command line.
cypherus