Get the App
SLTechnology News&Howtos  ›  Database  › 

What is the implementation of wildcard * matching function in MYSQL query

Shulou Source: shulou.com Published: 2022-05-31 13:33:07 10月02日 Update

What is the implementation of the wildcard * matching function in MYSQL query? I believe many inexperienced people are at a loss about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Today, when I was sending an intra-site short message, I encountered a function, that is, to search for wildcard characters of the account.

The following situations may be used in the search

Sunzhongshan string no wildcard search

Sun* begins with sun

* shan ends with shan

Sun*shan begins with sun and ends with shan

* zhong* contains zhong

Sun*ong* starts with sun and contains ong

The first thing to think about is to make a judgment, what to do if it starts with * and how to end with *. Then use * as the array separator to deal with it one by one.

This requires a large number of functions to make judgments, which is a bit of a waste of resources. So I decided to rethink my thinking.

. . .

The last code written by sun* is usename LIKE 'sun%'.

* the last code written by zhong* is usename LIKE'% zhong%'

..

In this way, I wonder if I can replace * with% and then deal with it.

Take sun*zhong* as an example, it can be replaced by sun%zhong%, which seems to be achievable, but it is difficult to split and convert.

Finally, the test shows that it can be replaced with%,%, and the replacement is sun%,%zhong%,%.

Then the trim () function is used to deal with the extra% symbols before and after.

Then split it into an array with explode (). The above string is array (0 = > sun%,1= >% zhong%,2= >).

It is also easy to deal with spaces, and skip it directly when processing in a loop.

Completed example code:

Tags: Dealing with that is wildcards beginnings functions codes times searches queries bad examples content functions characters strings arrays methods more problems at a loss Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Xiaomi vpn Shulou Tech Info Docker