SQL statement for values with spaces in the MySQL query field

Source: Internet
Author: User
Tags mysql query table name trim

(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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.