Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2009
    Posts
    4

    Unanswered: Creating a table from other tables

    Hello everyone,

    I have tw0 tables and both have an ID field in them.

    Say table 1 contains ID's 1,2,3,4,5,6,7,8,9
    and
    Table 2 contains ID's 2,3,4,5,6

    Now i want to create a new table from these tables which should have an ID field with the ID's which are present in Table 1 and NOT in Table 2.

    Such that Table 3 contains the following entries

    ID's - 1,7,8,9

    each entry should be unique and not repeated in Table 3

    In short
    Table 3 = Table 1 - Table 2


    Any help is appreciated..
    Thanks in advance...

  2. #2
    Join Date
    Apr 2008
    Location
    Iasi, Romania
    Posts
    561
    Provided Answers: 2
    SELECT *
    FROM table1
    WHERE ID NOT IN
    (SELECT ID FROM table2)
    Florin Aparaschivei
    DB2 9.7, 10.5 on Windows
    Iasi, Romania

  3. #3
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    CREATE TABLE table3
    SELECT table1.*
      FROM table1
    LEFT OUTER
      JOIN table2
        ON table2.ID = table1.ID
     WHERE table2.ID IS NULL
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

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