SQL Insert Into question

Discussion in 'MySQL' started by WhatiFind, Jan 26, 2006.

  1. #1
    I've got a sql database with these fields:

    database 1
    id | title | keywords | description | active | parentId

    I made a text file dump of this compete database, now I want to manually insert this data into database 2 with an SQL query in phpmyadmin running
    INSERT INTO `cat` VALUES (id, 'title', 'keywords ', 'description ', 'active ', parentId);
    , the only problem is that the structure of the other database is totally different:

    database 2
    id | parentId | title | description | active | frontpage | lastUpdate

    Is there an easy way to insert the old data from database 1 into database 2 into the right fields? There are over 1000 entries in this database.

    I'm not really familiar with sql databases, tried some things but it didn't work. If the fields keywords is lost that doesn't matter, not really important.

    Thanks in advance.
     
    WhatiFind, Jan 26, 2006 IP
  2. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #2
    When I migrate tables I write PHP code to do it.

    First select all from the old table. Loop through each record. Re-jig the order and add 'NULL' or whatever other default value for the new fields and then INSERT into the new table for each row.
     
    T0PS3O, Jan 26, 2006 IP
  3. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #3
    You can specify the ordering of the fields in the SQL e.g.
    
    INSERT INTO `cat` 
        (id, parentId, title, description, active)
    VALUES 
        (id, parentId, 'title', 'description ', 'active ');
    
    Code (sql):
     
    dct, Jan 26, 2006 IP
    WhatiFind and l234244 like this.
  4. WhatiFind

    WhatiFind offline

    Messages:
    1,789
    Likes Received:
    257
    Best Answers:
    0
    Trophy Points:
    180
    #4
    Thanks dct! Got it working. It was after all quite simple, thanks for pointing me in the right direction.
     
    WhatiFind, Jan 26, 2006 IP
  5. linkstraffic

    linkstraffic Well-Known Member

    Messages:
    388
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    133
    #5
    From one table to the other, you will need this:

    Mysql - INSERT ... SELECT

    http://dev.mysql.com/doc/mysql/en/INSERT_SELECT.html
    With INSERT ... SELECT, you can quickly insert many rows into a table from one or many tables.

    For example:
    INSERT INTO tbl_temp2 (fld_id)
    SELECT tbl_temp1.fld_order_id
    FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;
     
    linkstraffic, Jan 30, 2006 IP