(1) MySQL replace function
Syntax: replace (object,search,replace)
Replaces all occurrences of search in object with replace
The code is as follows |
|
Update ' News ' Set ' content ' =replace (' content ', ', ', ')//clear the spaces in the content field in the news table so you can use the like query directly. |
(2) MySQL trim function
Syntax: Trim ([{BOTH | Leading | Trailing} [REMSTR] from] str)
The following examples illustrate:
The code is as follows |
|
mysql> SELECT TRIM (' phpernote '); -> ' Phpernote ' Mysql> SELECT TRIM (leading ' x ' from ' xxxphpernotexxx '); -> ' phpernotexxx ' Mysql> SELECT TRIM (BOTH ' x ' from ' xxxphpernotexxx '); -> ' Phpernote ' Mysql> SELECT TRIM (trailing ' xyz ' from ' phpernotexxyz '); -> ' Phpernotex ' |
When we use SQL queries, if the value of this field in the database contains a space (inside the string, not the end), or the string we query has spaces in the middle, and there are no spaces in the field. Then we're probably not going to find anything. If you have one of the following tables:
Table name
ID URL Title Content
In this table, the title section of the first and second records has a space, and if we don't know, or because of the inability of the content, there is a lot of uncertainty about whether or not there are spaces:
The code is as follows |
|
SELECT * FROM table where title = ' Li Yang technology blog '; SELECT * FROM table where title like '% Li Yang technology blog% '; |
The above two SQL can not query the correct results, then how should we write it?
Please look below:
The code is as follows |
|
SELECT * FROM table where trim (replace (title, ', ', ') = Trim (replace (' Li Yang Technology blog ', ', ')); SELECT * FROM table where trim (replace (title, ",")) Like trim (replace ('% Li Yang Technical blog% ', ', ')); |
The effect of trim is to remove the whitespace from the end of the string, and replace to remove the space inside the string.
This can be correctly matched, if you do not want to give MySQL too much pressure, the conditional part of the processing of space can be implemented in the program.