Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

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:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report