I've the following database
my_table [id,name,address,phone] with a lot of entries and i would like to delete the Duplicate data where any just any duplicated phone will results in deleting.
Here is my try but shows error
Inside my sql file
CREATE TABLE `my_table` ( `id` int(10) NOT NULL default '0', `name` varchar(255) NOT NULL default '', `address` varchar(255) NOT NULL default '', `phone` varchar(255) NOT NULL default '', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; INSERT INTO `my_table` VALUES (1, 'Albert', 'EGYPT', '202020'); INSERT INTO `my_table` VALUES (2, 'John', 'USA', '984731'); INSERT INTO `my_table` VALUES (3, 'Steve', 'Romabia', '202020'); INSERT INTO `my_table` VALUES (4, 'Albert', 'EGYPT', '343354'); very clear that row of id 1 and 3 have same phone number then will remove duplicated so results be
INSERT INTO `my_table` VALUES (1, 'Albert', 'EGYPT', '202020'); INSERT INTO `my_table` VALUES (2, 'John', 'USA', '984731'); INSERT INTO `my_table` VALUES (3, 'Albert', 'EGYPT', '343354'); How is my try
I just have added to the sql file the following
1- created new table to get distinct
CREATE TABLE my_temp(id VARCHAR(10), name VARCHAR(255), address VARCHAR(255), phone VARCHAR(255)); INSERT INTO my_temp(id,name,address,phone) SELECT DISTINCT id,name,address,phone FROM my_table; 2- Delete entries from real table my_table
DELETE FROM my_table; 3- Getting entries back from my_tamp table to real my_table
INSERT INTO my_table(id,name,address,phone) SELECT id,name,address,phone FROM my_temp; 4- Drop useless table my_temp
DROP TABLE my_temp; Now my problem
it will still show me the same
INSERT INTO `my_table` VALUES (1, 'Albert', 'EGYPT', '202020'); INSERT INTO `my_table` VALUES (2, 'John', 'USA', '984731'); INSERT INTO `my_table` VALUES (3, 'Steve', 'Romabia', '202020'); INSERT INTO `my_table` VALUES (4, 'Albert', 'EGYPT', '343354'); because it won't consider no duplicate since they differ in id,name,address
so how i can adjust my way so that it delete duplicate if any only if there is duplicate in phone without care of id,name,address if differ or not
Hint
i've adjust this part
INSERT INTO my_temp(id,name,address,phone) SELECT DISTINCT phone FROM my_table; but it will insert into my_temp table
INSERT INTO `my_table` VALUES (1, 'null', 'null', '202020'); INSERT INTO `my_table` VALUES (2, 'null', 'null', '984731'); INSERT INTO `my_table` VALUES (3, 'null', 'null', '343354'); so i won't be able to get the data back to my_table