title: "MySQL Reverse Fuzzy Search"
description: "I'm sure everyone has used MySQL's fuzzy search, but have you ever used reverse fuzzy search?"
image: cover.png
date: "2018-04-01T21:56:48+0800"
slug: "2018040121"
categories:
- Database
tags:
- Fuzzy
- Database
- MySQL
Today, I wrote a demo for WeChat replies, and encountered this scenario.
1. Receive a specific message
2. Reply with specific content based on keywords stored in the database
3. Not an exact match, the database can perform fuzzy matching
When it comes to fuzzy matching, everyone must think of like
. Assuming the table structure is as follows
id keyword reply
1 David He is David
2 David King He is David
If the input keyword is David
, it can find all keywords containing David
At this point, the SQL
we write will definitely be like this:
select * from table_name where keyword like '%David%
Let's consider a different scenario, where we only store one record in the database. Regardless of whether the user inputs David or David King, we will reply with the same content. In this case, we only need to perform a reverse fuzzy search
Table data is as follows
id keyword reply
1 %David% He is David
Afterwards, the SQL
we write will be as follows to complete the reverse fuzzy search
select * from table_name where 'David' like keyword