views:

21

answers:

1

I am trying to rectify a previous database creation with tables that contains data that needs to be saved. Instead of recreating a completely new database since some of the tables are still reusable, I need to split a table that exists into 2 new tables which I have done. Now I am trying to insert the data into the 2 new tables and because of duplicate data in the old table I am having a hard time doing this.

Old table structure:

ClientProjects   clientId PK
                 clientName 
                 clientProj
                 hashkey    MD5 (clientname and clientProj)

new table structures:

client           clientId PK
                 clientName

projects         queryId   PK
                 clientId  PK
                 projectName 

I hope this makes sense. The problem is that in the old table for example you have clients with multiple clientIds.

+1  A: 

Supposing your clientName is unique you could so something like:

INSERT client (clientId, clientName)
    SELECT MAX(clientID), clientName FROM oldTable GROUP BY clientName;

INSERT project (clientId, projectName)
    SELECT n.clientId, o.projectName from client n
        INNER JOIN oldTable o on o.clientName = n.clientName;
Francisco Soto

related questions