If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Creating a table from other tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-07-09, 02:24
Hunky Hunky is offline
Registered User
 
Join Date: Apr 2009
Posts: 4
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...
Reply With Quote
  #2 (permalink)  
Old 08-07-09, 03:14
aflorin27 aflorin27 is offline
Registered User
 
Join Date: Apr 2008
Location: Iasi, Romania
Posts: 317
SELECT *
FROM table1
WHERE ID NOT IN
(SELECT ID FROM table2)
__________________
Florin Aparaschivei
Iasi, Romania
Reply With Quote
  #3 (permalink)  
Old 08-07-09, 06:03
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
CREATE TABLE table3
SELECT table1.*
  FROM table1
LEFT OUTER
  JOIN table2
    ON table2.ID = table1.ID
 WHERE table2.ID IS NULL
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On