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;
Related
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.
This question already has answers here:
Query with multiple values in a column
(4 answers)
How can I use comma seperated value stored in variable in MYSQL IN condition?
(1 answer)
Closed 1 year ago.
I have 3 tables:
TABLE 1
userID | userName
TABLE 2
topicID | topicName
TABLE 3
userID | topicID
(One user can have several topics asigned)
I am receiving a string of topicNames and a userID. I need to insert this data into table 3 but first I need to get the topicIds from table 2. Currently my solution would be to do a select query to get the topicIds for the topicNames (from table 2) and then use that to insert to Table 3.
Is there a way to do the insertion and the mapping in one query?
#string := 'topicName,userID'; -- for example, 'my super-topic,123'
INSERT INTO table3 (userID, topicID)
SELECT SUBSTRING_INDEX(#string, ',', -1), topicID
FROM table2
WHERE topicName = SUBSTRING_INDEX(#string, ',', 1);
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;
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.
This question already has answers here:
Remove duplicate rows in MySQL
(26 answers)
Closed 5 years ago.
I have a table with a few thousand rows. The table contains two columns, name and email. I have several duplicate rows, for example:
John Smith | john#smith.com
John Smith | john#smith.com
Erica Smith | erica#smith.com
Erica Smith | erica#smith.com
What would be the easiest way to delete all duplicate results. For example, such that the table's content would = SELECT name, DISTINCT(email) FROM table.
You could pretty easily do this by selecting that query into another table, then renaming it to replace the original.
CREATE TABLE `table2` (
`name` varchar(255),
`email` varchar(255),
UNIQUE KEY `email` (`email`));
INSERT INTO `table2` SELECT `name`, DISTINCT(`email`) FROM `table`;
RENAME TABLE `table` TO `table1`;
RENAME TABLE `table2` TO `table`;
Note that this CREATE should be adjusted to your actual table format. I added the unique key on the email field as a suggestion on how you would prevent duplicates in the first place.
Alternatively, you could loop over this
DELETE FROM `table`
WHERE `email` IN (
SELECT `email` FROM `table` GROUP BY `email` HAVING count(*) > 1
) LIMIT 1
Which would delete one duplicate record per call. The importance of the limit is to not remove both rows for any duplicate
The easiest way would be to copy all distinct values into a new table:
select distinct *
into NewTable
from MyTable
DELETE FROM table
WHERE id
NOT IN
(SELECT A.id
FROM
(
SELECT name,MAX(id) AS id
FROM table
GROUP BY name
) A
)
Add an auto-increment field to the table. I believe that when you add it, it will be 'backfilled' for you. Since MySql doesn't allow a delete based on a subquery against the same table, the easiest solution is to then dump the whole dataset into a temptable for use in processing. Assuming you called the new field RowId and the temp table tempTable, you can then use code like this:
DELETE FROM NameAndEmail
LEFT JOIN
( SELECT name, email, Max(RowId) as MaxRowId
FROM temptable
GROUP BY name, email
) as MaxId
WHERE NameAndEmail.Email = MaxId.Email
and NameAndEmail.Name = MaxId.Name
and NameAndEmail.RowId <> MaxId.RowId
Add a unique index
The simplest way to clean up a table with duplicate data - is to just add a unique index:
set session old_alter_table=1;
ALTER IGNORE TABLE `table` ADD UNIQUE INDEX (name, email);
Pay particular attention to the first sql statement, without it the IGNORE flag is ignored and the alter table statement will fail with an error.