Question about string spaces in query conditions in MySQL _ MySQL

Source: Internet
Author: User
BitsCN.com

MySQL's question about string spaces in query conditions

Assume that the current mysql database has a table: sysuser

There is a field: sysUseName

The record content in the field is: robin

See the following two SQL statements:

SELECT * from sysuser s where s. sysUseName = 'Robin'

SELECT * from sysuser s where s. sysUseName = 'Robin space'

SELECT * from sysuser s where s. sysUseName = 'Robin space'

All three statements can query the record.

The official document says that the MySQL checking rule belongs to PADSPACE. comparing CHAR and VARCHAR values ignores trailing spaces, and does not matter with server configuration or MySQL version.

MySQL installation directory has a doc Directory (Windows ), the section "Data Types-> String Types-> The CHAR and VARCHAR Types" in The section describes The problem and related examples. (if The version is different, The chapter numbers may change, see the title): All MySQL collations are of type PADSPACE. this means that all CHAR and VARCHAR values in MySQL are compared without regard to any trailing spaces.

The question is: how do I need to precisely match the content of robin? Suppose there is a login function. I want the user to enter 'Robin 'to log on, but enter 'Robin space' but cannot log on. how can this problem be achieved.

The solution is as follows:

SELECT * from sysuser s where s. sysUseName = BINARY 'Robin'

BINARY is not a function, but a type conversion operator. it is used to force the string following it to be a BINARY string. it can be understood as case-sensitive and exact match during string comparison.

In addition, because some MySQL, especially 4. * used to have inaccurate Chinese search results, binary can be added during the search.

BitsCN.com

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.