How to create new table from a distinct table in sql? [duplicate] - mysql

This question already has answers here:
Create table in MySQL that matches another table?
(5 answers)
Closed 3 years ago.
i currently have a table named my_table, like:
now when i run this query:
SELECT DISTINCT Country,Zipcode,Plan_Id FROM my_table;
it's gives me a table without those repeated rows.
now i want that output in a new table, how do i do?

You need CREATE TABLE . . SELECT :
CREATE TABLE table
AS
SELECT DISTINCT Country, Zipcode, Plan_Id
FROM my_table;
By this way you will get data into new table. If you already have a new table then use INSERT INTO . . . SELECT instead :
INSERT INTO table (Country, Zipcode, Plan_Id)
SELECT DISTINCT Country, Zipcode, Plan_Id
FROM my_table;

Related

Copy from table to another table without specific values [duplicate]

This question already has answers here:
Avoid duplicates in INSERT INTO SELECT query in SQL Server
(11 answers)
Closed 1 year ago.
In sql I have one blank table and second one with values
In me second one I have column with values like
poland-data
russia-data
usa-data
england-data
poland-data-hr
england-data-hr
england-hr
poland-hr
I want to copy to the blank table column with values without 'hr' from table nr.2
for example i only want to see in that table
poland-data
russia-data
usa-data
england-data
You can try following approach:
SELECT distinct REPLACE(col1 ,'-hr', '') as col1
INTO TableTwo
FROM TableOne
It's not entirely clear what you are asking. Is this a specific case of something that will have to be done many times with variable columns? If not, you should be able to just to
CREATE TABLE TABLE_2 AS
SELECT poland_data,
russia_data,
usa_data,
england_data
FROM TABLE_1;
Or
INSERT INTO TABLE_2
SELECT poland_data,
russia_data,
usa_data,
england_data
FROM TABLE_1;
If you already have the table created.

SELECT * INTO <non existent table>- MYSQL [duplicate]

This question already has answers here:
SELECT INTO in MySQL
(2 answers)
Closed 5 years ago.
I'm trying to move a select statement's result into a new non-existent table but not able to figure how.
In MS SQL, I would be following the below,
SELECT * INTO <NON_EXISTING_TABLE> FROM
(
SELECT * FROM TABLE1 A
JOIN TABLE2 B
ON A.DescriptionNo = B.DescriptionNo
WHERE A.DescriptionNo =1) A
When i quickly looked it up , I can see only answers to insert data into an existing table but not dynamically create a new table with the result of the statement.
Please advice !
If you want to create a new table from a select, you can use this syntax:
create table new_table as
select *
from existing_table
where ...
This solution showed above works perfect also for selected rows. For example I am creating demonstration rows for my nice2work project, and this works perfect.
CREATE TEMPORARY TABLE tmptable SELECT * FROM myTable WHERE id=500;
UPDATE tmptable SET id = 0;
UPDATE some fields I need to change
INSERT INTO myTable SELECT * FROM tmptable;
DROP TABLE tmptable;
// You can use this same also directly into your code like (PHP Style)
$sql = "CREATE TEMPORARY TABLE tmptable SELECT * FROM myTable WHERE id=500;
UPDATE tmptable SET id = 0;
UPDATE some fields I need to change
INSERT INTO myTable SELECT * FROM tmptable;DROP TABLE tmptable;";

MySQL insert into table with values and where [duplicate]

This question already has answers here:
MySQL: Insert record if not exists in table [duplicate]
(16 answers)
Closed 7 years ago.
I have two tables event and guest and an eventGuest table that joins them together and has some information about the guest (like if they attended etc) and am trying to insert into the eventGuest table without creating a duplicate sort of like:
insert into eventGuest(eventID, GuestID, attended)
values(iEventID, iGuestID, bAttended)
where (select count(*) from eventGuest where eventID = iEventID and guestID = iGuestID) = 0
Copy one table data to another :-
INSERT INTO TARGET_TABLE (`col1`,`col2`) SELECT `col1`,`col2` FROM SOURCE_TABLE;
You should use INSERT INTO ... SELECT if you want to insert values from a table into another:
INSERT INTO eventGuest(eventID, GuestID, attended)
SELECT iEventID, iGuestID, bAttended
FROM Anothertable t
where NOT EXIST(select 1
from eventGuest e
where e.eventID = t.iEventID
and e.guestID = t.iGuestID);
Or, if you want to insert into the same table if the values of eventid and iGuestid doesn't exist, you can do this:
INSERT INTO eventGuest(eventID, GuestID, attended)
SELECT *
FROM ( SELECT 'eventid', 'guestid', 'somevalue' ) AS t
WHERE NOT EXISTS (
SELECT 1 FROM eventGuest e
WHERE e.eventID ='eventid'
and e.guestID = 'guestid'
) LIMIT 1;
Please do add a unique constraint in the eventGuest table for both eventid and guestid and use INSERT IGNORE or REPLACE command to insert the new data.

How to delete duplicated columns in mysql table [duplicate]

This question already has answers here:
Remove duplicate rows in MySQL
(26 answers)
Closed 7 years ago.
I want to delete duplicate columns in mysql.
Here is sample:
person------city
Tom--------NY
Tom--------NY
Jhon-------LA
Desired output like this
person------city
Tom--------NY
Jhon-------LA
How can we do this in MySQL?
If you do not have primary key in your table, then it is easier to create new table with distinct values and then insert back into original table (assuming your table name is "ORIGINAL_TABLE" ) :
Create table Table2 as Select distinct person, city from ORIGINAL_TABLE;
truncate table ORIGINAL_TABLE;
insert into ORIGINAL_TABLE (person, city ) Select person, city from Table2
drop table Table2;

Add the data of two similar tables in Mysql

I have two similar tables with same columns. I just want to add the data of the column of one table with the corresponding data of the column of another table. Thus, form a new table which consists of the sum of individual data of both the tables.
Would you please help me?
something like this?
INSERT INTO myTable
SELECT
s.salary + t.salary AS salary,
s.name
FROM table_name s
JOIN table_name_2 t ON t.name = s.name -- or whatever parameter you use to join the two tables on
you can do like
insert into newTable select * from table1;
insert into NewTable select * from table2;
this will insert two tables data to new Table.