Create table as select....

Posted on 2004-11-09
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
Question by:Marmeladov
    LVL 13

    Accepted Solution


    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.
    LVL 50

    Expert Comment

    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...


    Author Comment

    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...
    LVL 13

    Expert Comment

    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!!)

    Author Comment

    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.


    Expert Comment

    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
    LVL 10

    Expert Comment

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    794 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now