还真没注意这个问题,原来的理解和楼主是一样的,但官网文档上是这样写的 https://dev.mysql.com/doc/refman/5.7/en/char.html If a given value is stored into the CHAR(4) and VARCHAR(4) columns, the values retrieved from the columns are not always the same because trailing spaces are removed from CHAR columns upon retrieval. The following example illustrates this difference:
The length of a CHAR column is fixed to the length that you declare when you create the table. The length can be any value from 0 to 255. When CHAR values are stored, they are right-padded with spaces to the specified length. When CHAR values are retrieved, trailing spaces are removed unless the PAD_CHAR_TO_FULL_LENGTH SQL mode is enabled.可以试着先执行 set session sql_mode = ’ PAD_CHAR_TO_FULL_LENGTH‘再执行length函数试试看结果
https://dev.mysql.com/doc/refman/5.7/en/char.html
If a given value is stored into the CHAR(4) and VARCHAR(4) columns, the values retrieved from the columns are not always the same because trailing spaces are removed from CHAR columns upon retrieval. The following example illustrates this difference: