How do I add results from one MySQL tale to another?

Discussion in 'MySQL' started by egdcltd, Aug 5, 2006.

  1. #1
    I want to do the following:

    SELECT column1, column2, column3 etc FROM table_name

    then INSERT column1, column2, column3 etc INTO table_name2

    I can get the results from the first table, but I don't know really how to insert those results into the second table.
     
    egdcltd, Aug 5, 2006 IP
  2. Angelus

    Angelus Well-Known Member

    Messages:
    1,622
    Likes Received:
    52
    Best Answers:
    0
    Trophy Points:
    138
    #2
    Put your results from first table into an array then put that array into second table :)

    p.l.u.r.
     
    Angelus, Aug 5, 2006 IP
  3. egdcltd

    egdcltd Peon

    Messages:
    691
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I thought it was something like that, but I've never used arrays before. Could you show how to do it please?
     
    egdcltd, Aug 5, 2006 IP
  4. sandossu

    sandossu Guest

    Messages:
    2,274
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    $sql = "select * from table_name";
    $query = mysql_query($sql);
    $row = mysql_fetch_assoc($query);
    $sql = "insert into table_name2(column1,column2,column3) values('".$row['column1']."','".$row['column2']."','".$row['column2']."')";
    mysql_query($sql);
    
    PHP:
    thank should work and you can put this in a for or something
     
    sandossu, Aug 5, 2006 IP
  5. egdcltd

    egdcltd Peon

    Messages:
    691
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks. Think I need to update my PHP book.
     
    egdcltd, Aug 5, 2006 IP
  6. Angelus

    Angelus Well-Known Member

    Messages:
    1,622
    Likes Received:
    52
    Best Answers:
    0
    Trophy Points:
    138
    #6
    Something like sandossu wrote. I'm not a programmer so listen to others ;)

    p.l.u.r.
     
    Angelus, Aug 5, 2006 IP