我有一个不同寻常的问题,这让我现在卡住了
表字段包括:
id bigint 20
name varchar 255
desc text有许多具有相同名称和desc的记录,但是desc在单词之间有一些额外的空格
喜欢
1 't1' 'hello world'
2 't2' 'hello world'我需要找到那些具有相似数据的行
我怎么才能找到这些,谢谢。
发布于 2011-01-24 21:18:05
这已经很接近了。假设:
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| d | text | YES | | NULL | |
| id | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+然后这个查询:
select x.id,x2.id,x.d,x2.d from x left join x as x2 on replace(x.d," ","") = replace(x2.d," ","") and x.id != x2.id having !(x2.id is null);获取重复的行。如果你有"Helloworld“(即没有空格),并且你不想让它匹配,它就会失败。
发布于 2011-01-24 20:35:10
除非需要保留原始数据,否则最好在插入时创建/更新记录,而不是在比较时再进行。
也就是说,您可以这样做
SELECT id, name, desc, REPLACE(desc, ' ', ' ') as replaced
xx x <--note the number of spaces
FROM table
GROUP replaced
HAVING replaced > 1这可能并不完美,您必须多次调整替换部分,但这应该可以让您开始使用。
https://stackoverflow.com/questions/4781965
复制相似问题