常用函数有char_length
、length
、bit_length
,它们针对不同的字符编码处理方式不一样。
总的来说: char_length:
不管汉字还是数字或者是字母都算是一个字符。length
不管汉字还是数字或者是字母都算是一个字符。bit_length
字符在当前编码下存储,所占的bit,也就是length
*8
GBK 编码
select char_length('中国'); -- 2
select length('中国'); -- 4
select length('china'); -- 5
select bit_length('中国'); --32
- 1
- 2
- 3
- 4
- 5
- 6
- 7
UTF-8 编码
select char_length('中国'); -- 2
select length('中国'); -- 6
select length('china'); -- 5
select bit_length('中国'); -- 48