Mysql can use the char_lengthlengthbit_length functions to obtain the string length. However, the usage of the three functions is different. This article introduces the difference between mysqlchar_lengthlengthbit_length and some friends can refer to it. There are two situations:
1. GBK encoding
-- Both Chinese and Western characters are treated equally, so the total number of characters is 2.
Select char_length ('China ');
-- One Chinese character occupies 2 bytes, so the total byte length is 4
Select length ('China ');
-- One English character occupies 1 byte, so the total length of the byte is 5
select length('china');
-- One Chinese character occupies 2 bytes, and one byte occupies 8 digits, so the total bit length is 32
Select bit_length ('China ');
2. UTF-8 coding
-- Both Chinese and Western characters are treated equally, so the total number of characters is 2.
Select char_length ('China ');
-- One Chinese character occupies three bytes, so the total length of the byte is 6.
Select length ('China ');
-- One English character occupies 1 byte, so the total length of the byte is 5
select length('china');
-- One Chinese character occupies 3 bytes, and one byte occupies 8 digits, so the total bit length is 48
Select bit_length ('China ');
Related reading:
Mysql length (get string length) function instance analysis
Mysql char_length () get the string length
Mysql BIT_LENGTH () obtains the bit length of a string.