Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2006
    Posts
    87

    Unanswered: compare and insert data on two tables

    Hi everybody...
    looking for a way to compare and insert data on two tables..

    I have two tables

    Tbl_email1
    emailID email
    1 info@sample1.com
    2 info@sample2.com
    3 info@sample3.com

    tbl_email2
    emailID email
    1 info@sample1.com
    2 info@sample4.com
    3 info@sample5.com

    I'm trying to compare tbl_email2 (email filed) with tbl_email1 (email field)
    if the record exist it it does nothing if not it adds the email field in tbl_email1
    the result would be

    Tbl_email1
    emailID email
    1 info@sample1.com
    2 info@sample2.com
    3 info@sample3.com
    2 info@sample4.com
    3 info@sample5.com

    thanks

  2. #2
    Join Date
    Jan 2004
    Location
    In a large office with bad lighting
    Posts
    1,040
    What have you come up with so far?

    -- This is all just a Figment of my Imagination --

  3. #3
    Join Date
    Aug 2006
    Posts
    87
    SELECT email FROM tbl_email2 WHERE email=(SELECT email FROM dbo.tbl_email1 WHERE email=info@sample1.com)

    if it exist from the above query then I do nothing else if it doesn't exist i insert the the email with this query below

    INSERT into tbl_email1 (email)
    VALUES(info@sample2.com)

    This is the manual procedure wherein I have to compre it one by one what would be helpful is how to automate it with a more complex query..

    thanks

  4. #4
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Code:
    INSERT INTO first_table (email_field)
    SELECT email_field
    FROM   second_table
    WHERE  email_field NOT IN (SELECT email_field FROM first_table)
    George
    Home | Blog

  5. #5
    Join Date
    Aug 2006
    Posts
    87

    great sql

    thanks george it has saved us a lot of time and it did it automatically with all 10 thousand plus records..

    again thanks

Posting Permissions

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