简体   繁体   中英

Mysql - Like & Regexp usage together

I am optimizing my query since full text search returns irrevelant result when there is numbers and repetitive keywords in text.

What I want to do is to extract numbers on text and add X amount of point to relevance when sorting the result.

Everything works smoothly besides one thing;

When I want to extract and prioritize result with number Z, it also counts other numbers that includes number Z in any part of it.

For Example;

Sample Data
###############
Text 55.A
Text 55_B
Text #55ABC
Text 551234.
Text 55677#
Text 556

    Query
###############
... CASE WHEN (myTable.title like "% 55%") THEN ...

Expected output
###############
Text 55.A
Text 55_B

Actual output
###############
Text 55.A
Text 55_B
Text #55ABC
Text 551234.
Text 55677#
Text 556

How could I use REGEXP with LIKE, there can be symbols and characters after number I have given.

Thanks in advance

You may use

REGEXP '([[:<:]]|_)55([[:>:]]|_)'

If you are using MySQL 8.x and newer that use ICU regex library use

REGEXP '(\\b|_)55(\\b|_)'

See the regex demo

The (\\\\b|_) matches a word boundary or a _ , the ([[:<:]]|_) matches a starting word boundary or _ and ([[:>:]]|_) matches a trailing word boundary or _ .

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