?
Solved

Create table as select....

Posted on 2004-11-09
7
Medium Priority
?
7,837 Views
Last Modified: 2007-12-19
How can I: create table tbl3 as select * from tbl1, tbl2 where tbl1.a = tbl2.a? tbl1 and tbl2 have similar structures, why i get the "duplicate column name" error for such a question. Could someone please give a solution to this. possibly a total different syntax is needed, butwhat?

Thanks in advance
0
Comment
Question by:Marmeladov
7 Comments
 
LVL 13

Accepted Solution

by:
bochgoch earned 750 total points
ID: 12532576
Try

1. create table tbl3 as select tbl1.* from tbl1, tbl2 where tbl1.a = tbl2.a
To create a table with the structure of tbl1
OR

2. create table tbl3 as select tbl2.* from tbl1, tbl2 where tbl1.a = tbl2.a
To create a table with the structure of tbl2

OR

3. Create table tbl3 as select tbl1.field1,tbl1.field2,tbl2.field2 ....etc.... from tbl1, tbl2 where tbl1.a = tbl2.a
To create a table with the structure comprised of columns from tbl1 & tbl2

I assume tbl1 & tbl2 contain columns with the same name, you need to specify which (or both) of the columns you want to use -- as in the syntax of example three.
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 12532644
do you want to create a VIEW over this select statement?

 create view tbl3 as select a.*,b.*
from tbl1 as a, tbl2 as b
where tbl1.a = tbl2.a


or do you really want to generate a new table?

which Database System is this for?

either way you'd need to name each column uniquely if you wanted to be able to reference them...

 
0
 

Author Comment

by:Marmeladov
ID: 12533090
I would like to create a new table, in order to simplify future searches and statistical output via a cgi. I would like it to work as suggested by bochgoch's third alternative, but as there are quite a few columns and those columns may change names in the future, i would like to select all columns from both tbl1 and tbl2 without specifying all column names (i am lazy).

btw the DB in question is MySQL

If there is no other way, i suppose i will have to specify all column names from tbl1 and tbl2 to create tbl3...
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 13

Expert Comment

by:bochgoch
ID: 12533179
Sorry, you will have to specify all column names...the god of SQL says that this is so.....

(your only alternative is to ensure that all column names are unique, but then you compromise the integrity of your database design...the choices we have to make!!)
0
 

Author Comment

by:Marmeladov
ID: 12533382
I guess the god of SQL gives no simple shortcuts then :( :P Anyway, thanks for your replys. Points go to bochgoch for quick answer with clear examples.

Thnx
0
 

Expert Comment

by:rooholamini
ID: 12664781
hi you can use this: create table tbl3 as select tbl2.* from tbl1, tbl2 where tbl1.a = tbl2.a
To create a table with the structure of tbl2
0
 
LVL 10

Expert Comment

by:peterside7
ID: 12674745
Just put the columns you want in the create table portion,and define in the select where they are coming from  
ex.:
create table tbl3 (col1, col1, col3 ...) as
select  tbl1.a,tbl1.b,tbl1.c,tbl2.d  
from tbl1, tbl2
where tbl1.a = tbl2.a
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
Creating a Cordova application which allow user to save to/load from his Dropbox account the application database.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question