1

I have a table called activation. In here I want to move records older than 30 days to another table. How to do this?

 SELECT * DATE_FORMAT(datetime, '%m/%d/%Y') 
 FROM 
 tablename 
 WHERE datetime <= DATE_SUB(SYSDATE(), INTERVAL 30 DAY)

I tried this query but it returns me an error:

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 'DATE_FORMAT(datetime, '%m/%d/%Y') FROM activation WHERE datetime <= DATE_SUB(SYS' at line 1

Alok Patel
  • 7,842
  • 5
  • 31
  • 47
Rajan
  • 2,427
  • 10
  • 51
  • 111
  • 2
    you are missing comma (`,`) between `*` and `DATE_FORMAT(datetime, '%m/%d/%Y')`, it should be `SELECT * ,DATE_FORMAT(datetime, '%m/%d/%Y') FROM .....` – kamal pal Jun 01 '16 at 12:09
  • do i have to pass anything in %m%d%Y ??? – Rajan Jun 01 '16 at 12:19
  • 1
    no, that's the format you want date to be appear at last column, take a look a date_format function, https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format – kamal pal Jun 01 '16 at 12:23
  • Okay thanks that solved my error but could you guide me how to move those records to another table – Rajan Jun 01 '16 at 12:26
  • if you already have table created, simply do `insert into newtable select columnnames from oldtable`, as stated in answers – kamal pal Jun 01 '16 at 12:35

2 Answers2

3

You can try like

INSERT INTO tablename2 select *, DATE_FORMAT(datetime, '%m/%d/%Y') from tablename1 WHERE datetime <= DATE_SUB(SYSDATE(), INTERVAL 30 DAY);

DELETE FROM tablename1 WHERE datetime <= DATE_SUB(SYSDATE(), INTERVAL 30 DAY);
Ben Swinburne
  • 25,669
  • 10
  • 69
  • 108
1

Example to how implement:

INSERT INTO Table2(LongIntColumn2, CurrencyColumn2)
SELECT LongIntColumn1, Avg(CurrencyColumn) as CurrencyColumn1 FROM tablename GROUP BY LongIntColumn1;

From: https://stackoverflow.com/a/74204/2925795

Community
  • 1
  • 1
user2925795
  • 400
  • 10
  • 29