我正在寻找查询,删除所有重复值。
Example Table:
1 ABC
2 BBB
3 DAC
4 ABC
5 AAA
6 ABC
output required
1 ABC
2 BBB
3 DAC
5 AAA
感谢您的帮助,我谷歌它找不到确切的解决方案。
如果要对DELETE
重复值进行实际操作(同时保留具有最低值的值id
),可以使用多表DELETE
语法:
DELETE a FROM tbl a
LEFT JOIN
(
SELECT MIN(id) AS id, name
FROM tbl
GROUP BY name
) b ON a.id = b.id AND a.name = b.name
WHERE b.id IS NULL
DELETE
在此处查看@fvu 答案:https ://stackoverflow.com/a/11249235/1166147
您可以创建一个unique index
将删除重复项并同时防止将来出现重复项(MySQL 5.1 或更高版本):
ALTER IGNORE TABLE 'YOURTABLE'
ADD UNIQUE INDEX somefancynamefortheindex (Col1ID, Col2)
假设Tab
您的表的名称包含重复项,请创建一个Tab_TMP
具有相同结构的临时表Tab
。
-- assuming `Tab` has same schema
CREATE TABLE Tab_TMP (
id INT(2) PRIMARY KEY,
name VARCHAR(8)
);
填写Table_TMP
所有Table
条目。
INSERT INTO Tab_TMP(id, name) SELECT id, name FROM Tab;
从 中删除条目Table
。
DELETE FROM Tab;
从使用中插入Table
条目。Table_TMP
SELECT DISTINCT
INSERT INTO Tab(name) SELECT DISTINCT name FROM Tab_TMP;
SELECT DISTINCT col1,col2,col3,..coln FROM 表名 INTO OUTFILE 'yourFilePathWIthFileName'
例如:SELECT DISTINCT username,usernumber,usersalary,dateOFJoining,timeofJoining,datetimeOfJoining FROM asdf INTO OUTFILE 'C:/Users/ahmed.m/Desktop/modify.txt';
LOAD DATA INFILE 'yourFilePathWIthFileName' INTO TABLE tableName 例如:LOAD DATA INFILE 'C:/Users/ahmed.m/Desktop/modify.txt' INTO TABLE asdf