简体   繁体   中英

Finding Partial Duplicates in MySQL (Keys with last 6 characters the same)

Got stuck on this one, know it should be simple.

But I have a list of unique IDs that looks like AB123456, XY584234, CDE987654. The last six characters mean something, so I need to find all rows that have the same last six characters as another (substring).

So ABCD1234 would match XYCD1234, and return the both of them. Need to run this on the whole database and get all the matches, preferably with the matches next to each other.

Is that possible?

You can do this with group by and right . The following returns a list of all ids that look similar:

select right(id, 6), group_concat(id)
from table t
group by right(id, 6);

You might want to add:

having count(*) > 1

If you don't want singletons.

请使用以下查询获取结果。

select * from tablename where right(columnname,6)= value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM