Insert values ​​from another table when creating a new table

Here is my FIDDLE .

I am trying to import data from an old table into a new table. The old table has many repetitions. In the new table, I can only insert DISTINCT emails. I cannot Insert a name as one and the same. Here is my code.

CREATE TABLE table_old(name VARCHAR(255), email VARCHAR(255));

INSERT INTO table_old (name, email) VALUES ('tom', 'tom@gmail.com'),
                                           ('peter', 'peter@gmail.com'),
                                           ('hitler', 'hitler@gmail.com'),
                                           ('haasan', 'haasan@gmail.com'),
                                           ('arun', 'arun@gmail.com'),
                                           ('tom', 'tom@gmail.com'),
                                           ('peter', 'peter@gmail.com'),
                                           ('hitler', 'hitler@gmail.com'),
                                           ('haasan', 'haasan@gmail.com'),
                                           ('arun', 'arun@gmail.com');

CREATE TABLE table_new AS (SELECT DISTINCT email FROM table_old );

So let me understand how to insert names in table_new in relation to the email column name.

+5
source share
3 answers

I think this is what you need:

CREATE TABLE table_new AS (SELECT name, email FROM table_old GROUP BY name, email);
+1
source

You can use the query below.

CREATE TABLE table_new AS (SELECT DISTINCT * FROM table_old );

+2
source

, , . :

CREATE TABLE table_new AS (SELECT email, max(name) FROM table_old group by email);

, , -.

+1

All Articles