2011-01-24 81 views
0

我有一个不寻常的查询,这让我找到卡住了MySQL查询,删除所有空格

表字段有:

id bigint 20 
name varchar 255 
desc text 

有具有相同名称和desc多条记录,但说明有一些在话犹如

之间

1 't1' 'hello world' 
2 't2' 'hello    world' 

我需要找到那些有SI行多余空格milar data

我怎样才能找到这些,谢谢。

回答

2

这非常接近。假设:

+-------+---------+------+-----+---------+-------+ 

| 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”(即没有空间)并且你不想匹配,它就会失败。

0

除非您需要保留原始数据,否则在插入时最好在创建/更新记录时完成,而不是在以后的比较时间。

话虽这么说,你可以不喜欢

SELECT id, name, desc, REPLACE(desc, ' ', ' ') as replaced 
             xx x <--note the number of spaces 
FROM table 
GROUP replaced 
HAVING replaced > 1 

可能不会完美,你将不得不几次调整更换部分,但这应该让你开始。