我正在寻找查询,删除所有重复的值。
Example Table:
1 ABC
2 BBB
3 DAC
4 ABC
5 AAA
6 ABC
output required
1 ABC
2 BBB
3 DAC
5 AAA
感谢您的帮助,我谷歌找不到确切的解决方案。
答案 0 :(得分:6)
如果要对重复值执行实际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
操作的demonstration 答案 1 :(得分:2)
请参阅@fvu答案:https://stackoverflow.com/a/11249235/1166147
您可以创建unique index
删除重复项并防止将来出现欺骗(MySQL 5.1或更高版本):
ALTER IGNORE TABLE 'YOURTABLE'
ADD UNIQUE INDEX somefancynamefortheindex (Col1ID, Col2)
答案 2 :(得分:0)
假设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;
答案 3 :(得分:0)
SELECT DISTINCT col1,col2,col3,.. coln FROM table name 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