Create table as select....

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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


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

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


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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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...

MarmeladovAuthor Commented:
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...
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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!!)
MarmeladovAuthor Commented:
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.

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
Just put the columns you want in the create table portion,and define in the select where they are coming from  
create table tbl3 (col1, col1, col3 ...) as
select  tbl1.a,tbl1.b,tbl1.c,tbl2.d  
from tbl1, tbl2
where tbl1.a = tbl2.a
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.