简体   繁体   中英

Locate similar entries in mySql

I was trying to locate duplicate entries in my sql table which I achieved it quite easily with the below query.

SELECT * FROM sds_bank_phrases INNER JOIN (Select bank_statement FROM sds_bank_phrases GROUP  BY bank_statement HAVING COUNT(bank_statement) > 1) dup ON sds_bank_phrases.bank_statement = dup.bank_statement;
  • Now what I am trying to look for is the entries which have same data but a full stop additionally added.
  • For example have bank_id 1 with bank_statement Yes
  • bank_id 2 with bank_statement Yes.
  • bank_id 3 with Yes, It has been edited.
  • so from the above example I just want to extract 1st two entries because they are the close once. just full stop is the difference.
  • I have a 20000 bank_statements and how do I extract such entries?

DB table

在此处输入图片说明

  • In the above image we can see the data Not known has duplicate entries.
  • The query posted can find all the entries with id 1274, 1353,1418,2455,3026 but cannot find 5637.
  • Because there is a full stop in that entry. Which is not considered to be duplicate.
  • Expected result would be to pull in the Not known as well.
  • It should ignore the ban_statement with id with 2787 because the bank_statement is different.

You could use a TRIM(TRAILING '.' FROM your_column);

SELECT * 
FROM sds_bank_phrases 
INNER JOIN (Select TRIM( TRAILING '.' FROM bank_statement ) as clean_bank_statement
FROM sds_bank_phrases GROUP  BY bank_statement HAVING COUNT(TRIM( TRAILING '.' FROM bank_statement )) > 1) dup 
    ON TRIM( TRAILING '.' FROM sds_bank_phrases.bank_statement ) = dup.clean_bank_statement;

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