Search code examples
mysqlcreate-table

Creating a table on the basis of a field


I have a MySql table which has about 100k rows. there is one field say id which contains numbers from 1-35. all these records fall in this range of id i.e. all these records have value of id column between 1-35.

Now i want to create another table which will have one row of each id. i.e the new table should have 35 rows only.

How to go about it ?


Solution

  • create table new_table (id int);
    insert into new_table
        select distinct id from big_table;
    

    Edit:

    You can create the new_table by outputting the big_table create script and changing the name.

    SHOW CREATE TABLE big_table;
    /* modify the name of the output and execute */
    insert into new_table
        select * from big_table group by id