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 > Compare two tables and save it to a 3rd table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-30-11, 20:55
madkitty madkitty is offline
Registered User
 
Join Date: Apr 2011
Posts: 18
Compare two tables and save it to a 3rd table

Hey Guys,

I have to compare two tables and save the matching result to a third table.
I can't seem to save anything even thou i have the matching result displayed on the screen.

Here is what I did :

SELECT * FROM table1, table2
-> WHERE table1.column2 = table2.column2
-> ORDER BY column2;

Then What?
I tried JOIN / NATURAL JOIN / INJOIN ..
Maybe I had an improper synthax .. neway nothing worked out so far I just ended up with a JOINT pain ..

Things you should know:
- Table 1, 2 and 3 have the exact same format.
- Column2 is defined as an integer
- Our DB exceed 3Gb..
- Sorry if this is baby step .. im started mysql 2 weeks ago
- Thanks Thanks Thanks

Help please .......
Reply With Quote
  #2 (permalink)  
Old 04-30-11, 21:01
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
when you say "save the matching result to a third table" what exactly does this mean?

you say all three tables have the same format, so why are you saving the results of table1 AND table2? that would be twice as many columns!!!

coould you please do a SHOW CREATE TABLE for these tables and explain exactly what columns you want saved

in fact, please explain why you want any results saved at all
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 04-30-11, 21:11
madkitty madkitty is offline
Registered User
 
Join Date: Apr 2011
Posts: 18
Table 1 and 2 are filled up with numbers
Table 3 is empty
Each table has 5 columns of integers

To create each table I just did this for each table :
CREATE table1 (col1 int, col2 int, col3 int, col4 int, col5 int);

What I need to have:
If between table 1 and 2, the second column has the same number then I should copy the whole line (from column 1 to 5) into table 3.

Sorry i copy pasted the wrong script, i had duplicated results at first so I changed it for :
SELECT * FROM table1
WHERE table1.col2 = table2.col2
ORDER BY col2;

but then How do I transfer this to table 3?...
Reply With Quote
  #4 (permalink)  
Old 04-30-11, 21:25
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
INSERT 
  INTO table3 
SELECT table1.* 
  FROM table1
INNER
  JOIN table2
    ON table2.col2 = table1.col2
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 04-30-11, 21:46
madkitty madkitty is offline
Registered User
 
Join Date: Apr 2011
Posts: 18
OMGoodness It works like charm

The synthax i wrote was different which I guess explains why it didnt work out ..


Okay now I have another question ..
What's the difference between INNER JOIN and NATURAL JOIN ?

Thanks a lot for your help ^^/
Reply With Quote
  #6 (permalink)  
Old 04-30-11, 22:28
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by madkitty View Post
What's the difference between INNER JOIN and NATURAL JOIN ?
the difference is in how you specify which columns to join on

__________________
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