现在的位置: 首页 > 综合 > 正文

mysql length、varchar(int) 详解

2018年01月24日 ⁄ 综合 ⁄ 共 513字 ⁄ 字号 评论关闭

在mysql语句中定义一个varchar(int)类型,这里int指的是字符的个数,而不是字节数。(这点和oracle的varchar相反)
例如:
create table test_sy (name VARCHAR(2));
insert into test_sy (name) values ("孙孙");
select * from test_sy;
结果:
孙孙
 
 
insert into test_sy (name) values ("sunying");
select * from test_sy;
结果:
su
 
在mysql中
length()取得是字符串的字节数,每个汉字3字节,字母是一个字节
char_length()取得是字符串的字符数,每个汉字、字母都是一个字符。
 
select LENGTH(name) from test_sy where name ="孙孙";
select CHAR_LENGTH(name) from test_sy where name ="孙孙";
结果:
6
2
 
select LENGTH(name) from test_sy where name ="su";
select CHAR_LENGTH(name) from test_sy where name ="su";
结果:
2
2

抱歉!评论已关闭.