Search code examples
mysqlleft-joincreate-table

error in create table from join in Mysql


I have 2 tables A and B which I want to join using a column id and create a third table C from that.
I performed this query:

create table C select * from ( A left join B using (id))T;

But it is giving error:

 You have an error in your SQL syntax; check the manual that corresponds to your   MySQL server version for the right syntax to use near 'T' at line 1

I don't know why the error is coming.


Solution

  • remove the following

    • parenthesis
    • FROM before SELECT
    • and the ALIAS

    also

    Tip, When creating a table with CREATE TABLE ... SELECT, make sure to alias any function calls or expressions in the query. If you do not, the CREATE statement might fail or result in undesirable column names.

    query,

    CREATE TABLE C 
    SELECT  * 
    FROM    A 
            LEFT JOIN B USING (id)