简体   繁体   中英

How to move data from one column to other in MySQL after every 24 hours using Java?

There are 10 columns. After every 24 hours, data present in column10 should move to column9, column9 data should move to column8, so on and so forth. How do I achieve this?

You need to something like this and update the records:

 take a variable that counts the number of records

  num = select count(*) from <tablename>  
  for (i=num;i>num;num--) {
     Insert into <tablename> (columns name comma separated...) 
     values (select (same  columns comma separated...) 
     from <same tablename> where id=num) where id=num-1;
  } 

and let me know, what about the first column.

Just create another table with a different name and inserting the data for column's in whatever way you want.

    -- creating myTable
Create table myTable  (column1 int (11),column2 int (11),column3 int (11),column4 int (11),column5 int (11),
column6 int (11),column7 int (11),column8 int (11),column9 int (11),column10 int (11) );
insert into myTable values(1,2,3,4,5,6,7,8,9,10);

    CREATE TABLE myTable_temp  (column1 int (11),column2 int (11),column3 int (11),column4 int (11),column5 int (11),
column6 int (11),column7 int (11),column8 int (11),column9 int (11),column10 int (11) );
insert into myTable_temp 
SELECT column2,column3,column4,column5,column6,column7,column8,column9,column10,column1
FROM 
    myTable ; 
    -- // Note: First create table then insert, "Create table select" will not work as required.

Then drop the original table and rename the _temp table to original table.

DROP TABLE tableName ;
RENAME table tableName_temp to tableName ;

select * from myTable ;
--  column1 column2 column3 column4 column5 column6 column7 column8 column9 column10    
--  2         3       4        5        6       7       8       9       10      1       

An for running the code After every 24 hours from JAVA you can use scheduler to call your function in which your SQL code is written.

new java.util.Timer().schedule( 
        new java.util.TimerTask() {
            @Override
            public void run() {
                // your SQL code here
            }
        }, 
        5000 
);

Or you can use the below link to see for more options.

java: run a function after a specific number of seconds

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM