Domanda

I have some historical data tables in my Mysql database.

I want to repeat a day's historical data for another day in the same table.

Table structure, with some sample data:

 Id | Date       | Value

  1 | 2012-04-30 |     5
  2 | 2012-04-30 |    10
  3 | 2012-04-30 |    15

I want to repeat those ids & values, but for a new date - e.g. 2012-05-01. i.e. adding:

  1 | 2012-05-01 |     5
  2 | 2012-05-01 |    10
  3 | 2012-05-01 |    15

I feel that there should be a straightforward way of doing this... I've tried playing with UPDATE statements with sub-queries and using multiple LEFT JOINs, but haven't get there yet.

Any ideas on how I can do this?

EDIT: To clarify... - I do NOT want to add these to a new table - Nor do I want to change the existing records in the table. - The ids are intentionally duplicated (they are a foreign_key to another table that records what the data refers to...).

È stato utile?

Soluzione

INSERT INTO yourTable
SELECT ID, "2012-05-01" As Date, Value
FROM yourTable
WHERE Date = "2012-04-31"

Usually, your ID would be an autoincrement though, so having the same ID in the same table would not work. Either use a different ID, or a different table.

Different ID (next autoincrement):

INSERT INTO yourTable
SELECT NULL as ID, "2012-05-01" As Date, Value
FROM yourTable
WHERE Date = "2012-04-31"

Different table (referring to original ID)

INSERT INTO yourTable_hist
SELECT NULL as ID, ID as old_ID, "2012-05-01" As Date, Value
FROM yourTable
WHERE Date = "2012-04-31"

Altri suggerimenti

Maybe something like this:

UPDATE Table1
SET Date=DATE_ADD(Date, INTERVAL 1 DAY)

Or if you want to insert them to a new table:

INSERT INTO Table1
SELECT
  ID,
  DATE_ADD(Date, INTERVAL 1 DAY),
  Value
FROM
  Table2
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top