Go Back   Cloud Computing > Support > MySQL Issues.
 

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 05-17-11, 07:57
BOD Member
 
Join Date: Nov 2010
Posts: 57
Default Want to compare 2 tables and save it to 3rd table

Hi

I have created three tables and now I want to compare two tables and save the matching results to existing third table. The problem is I cannot seem to save anything. Here is what I did

Quote:
SELECT * FROM table1
WHERE tableA.columnB = tableB.columnB
ORDER BY columnB;
Then I tried JOIN, NATURAL JOIN, and INJOIN .. Maybe I had an indecent syntax..anyway nothing worked out so far.

FYI : Table A, B and C have the exact similar format. Column B is defined as an integer. My database exceed 5Gb..

Reply With Quote
  #2 (permalink)  
Old 05-17-11, 08:14
Kristine Habeck's Avatar
BOD Member
 
Join Date: Nov 2010
Posts: 122
Default

First, why you are trying to save the matching results in to a third table? As you said, all three tables have the similar format, then what makes sense to save the results in table A and tables B? It would be twice as many columns. Can you elaborate what exact columns you want to save?
Reply With Quote
  #3 (permalink)  
Old 05-17-11, 08:19
BOD Member
 
Join Date: Nov 2010
Posts: 57
Default

Actually Table A and B filled with numbers and tables C is empty. FYI, each table has six columns of integers. To make each table I did this for each table

CREATE tableA(column1 int, column2 int, column3 int, column int, column5 int, column6);

What I want to see is, if between table A and B, the second column has the similar number, then I should copy the entire line (from column 1 to 6) into table C. So how can I transfer this to table third table C?
Reply With Quote
  #4 (permalink)  
Old 05-17-11, 08:25
Kristine Habeck's Avatar
BOD Member
 
Join Date: Nov 2010
Posts: 122
Default

You need to consider following solution >

Quote:
INSERT
INTO tableC
SELECT tableA.*
FROM tableA
INNER
JOIN tableB
ON tableB.columnB = tableA.ColumnB
Reply With Quote
  #5 (permalink)  
Old 05-17-11, 08:27
BOD Member
 
Join Date: Nov 2010
Posts: 57
Default

Thanks it works now. But now I have another question, what is the difference between INNER JOIN and NATURAL JOIN?
Reply With Quote
  #6 (permalink)  
Old 05-17-11, 08:29
Kristine Habeck's Avatar
BOD Member
 
Join Date: Nov 2010
Posts: 122
Default

The simple difference between both is how you specify which columns to join on.
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump


All times are GMT -6. The time now is 00:50.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
Copyright © 1999-2012, BODHost Ltd. All rights reserved.