jokey jokey - 23 days ago 7
MySQL Question

SQL: Delete fields value after an amount of time

Im new at SQL and Im currently using it for one of my projects.

I want to delete the data in a field eg. if it was not changed for 30 days.

I thought of something like this -

DELETE eventLog WHERE date < (lastChange - INTERVAL 30 DAY);



  1. eventLog is a varchar

  2. lastChange is a DATE



I tried it but I think it does not really worked.

Does somone has a better way to do that?

Thanks

Answer

DELETE does not remove values from a field. For varchar you want something like:

UPDATE tableName
SET eventLog = ''
WHERE ...

OR

UPDATE tableName
SET eventLog = NULL
WHERE ...

DELETE is used to remove entire rows from a table.

Also as for the last 30 days part see this:

MySQL Query - Records between Today and Last 30 Days