MySQL comparison operator, spaces

后端 未结 5 725
再見小時候
再見小時候 2021-01-11 23:04

If the database row is like this: country = \'usa\' and i query \"select * from data where country = \'usa \'\" it also returns this row. So its no

5条回答
  •  一整个雨季
    2021-01-11 23:53

    As mentioned in the manual:

    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.

    In the definition of the LIKE operator, it states:

    In particular, trailing spaces are significant, which is not true for CHAR or VARCHAR comparisons performed with the = operator:

    As mentioned in this answer:

    This behavior is specified in SQL-92 and SQL:2008. For the purposes of comparison, the shorter string is padded to the length of the longer string.

    From the draft (8.2 ):

    If the length in characters of X is not equal to the length in characters of Y, then the shorter string is effectively replaced, for the purposes of comparison, with a copy of itself that has been extended to the length of the longer string by concatenation on the right of one or more pad characters, where the pad character is chosen based on CS. If CS has the NO PAD characteristic, then the pad character is an implementation-dependent character different from any character in the character set of X and Y that collates less than any string under CS. Otherwise, the pad character is a .

    In addition to the other excellent solutions:

    select binary 'a' = 'a   '
    

提交回复
热议问题