jokey jokey - 1 year ago 58
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?


Answer Source

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

UPDATE tableName
SET eventLog = ''


UPDATE tableName
SET eventLog = NULL

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download