mysql> create table vc(v varchar(4),c char(4));
Query OK, 0 rows affected (0.06 sec)mysql> insert into vc values('ab ','ab ');
Query OK, 1 row affected (0.00 sec)mysql> select concat(v,'+'),concat(c,'+') from vc;
+---------------+---------------+
| concat(v,'+') | concat(c,'+') |
+---------------+---------------+
| ab + | ab+ |
+---------------+---------------+
1 row in set (0.00 sec)
varchar是可变的,反而没把空格去掉,而char是固定长度的,反而把空格去掉了,这是为什么啊?谢谢!
Query OK, 0 rows affected (0.06 sec)mysql> insert into vc values('ab ','ab ');
Query OK, 1 row affected (0.00 sec)mysql> select concat(v,'+'),concat(c,'+') from vc;
+---------------+---------------+
| concat(v,'+') | concat(c,'+') |
+---------------+---------------+
| ab + | ab+ |
+---------------+---------------+
1 row in set (0.00 sec)
varchar是可变的,反而没把空格去掉,而char是固定长度的,反而把空格去掉了,这是为什么啊?谢谢!
'ab' 'ab' 'ab '
'ab ' 'ab ' 'ab '
'' '' ' '
'abcd' 'abcd' 'abcd'
http://dev.mysql.com/doc/refman/5.1/en/char.htmlThe 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.
varchar可以保存结尾的空格,也可以正常取出来
char尾部要填充空格,所以就无法知道你存入的时候尾部是不是有空格。
mysql取出char类型时自动去掉了尾部的空格,象oracle就把空格也放在这个串里了。