Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    May 2010
    Posts
    215
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Copying unique row table to another table

    Suppose I have table1 and table2, table2 is exact duplicate of some table rows in table1. When table1 get new record, how can I insert the new record on table2. I know you can use the insert to put the new record of table1 to table2. But what I want is to check table1 if there are records that is not on table2, and if it does, insert that record on table2.

    To make it more clear table1 one has fname, lname and country fields and table2 is the duplicate of table1, but table2 does not have the country fields. What I want is if table one get new record, the new fname and lname record will be also inserted in table2.

    Below is q query i just not now how to continue it.

    SELECT fname,lname
    INTO table2
    FROM table1
    WHERE fname
    Last edited by Anishgiri; 07-07-2011 at 03:18 AM.

  • #2
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    Table 2 is redundant. you have the information you need in table one. If you only need first and last name query those, if you need country too then query it.

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,297
    Thanks
    23
    Thanked 612 Times in 611 Posts
    The easiest way to do the two things you want:
    I want is to check table1 if there are records that is not on table2, and if it does, insert that record on table2.
    A work around - backup table2 or if you don't know how rename it
    Code:
    RENAME TABLE table2 TO table3;
    Then create a new table2
    Code:
    CREATE TABLE table2 LIKE table3;
    Then populate it with the records of table1
    Code:
    SELECT fname,lname INTO table2 FROM table1;
    =========================================

    Next question
    What I want is if table one get new record, the new fname and lname record will be also inserted in table2.
    Find the place in the code where a new record is inserted to table1 and add a insert to table2. This will keep you up to date from this point on.
    Code:
    INSERT INTO table1 (fname, lname, country) VALUES ("First_name", "Last_name", "Living_place");
    INSERT INTO table2 (fname, lname) VALUES ("First_name", "Last_name");


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •