char_length()可用于显示字符串的长度。让我们看一个示例,获取包含为参数的字符串的长度。
mysql> select char_length('John');
以下是输出。
+---------------------+ | char_length('John') | +---------------------+ | 4 | +---------------------+ 1 row in set (0.00 sec)
该length()
方法可用于显示以字节为单位的字符串长度。在许多情况下,字符和字节的长度相同。
这是一个例子 length()
mysql> select length('Tim');
以下是输出。
+---------------------+ | length('Tim') | +---------------------+ | 3 | +---------------------+ 1 row in set (0.00 sec)
在length()
和CHAR_LENGTH()给出的字符串相同的结果。length()
当应用Unicode时,该函数给出不同的长度。假设列名具有unicode.Unicode中,每个字符占用2个字节。
让我们看一个例子-
mysql> create table LengthAndCharLengthDemo -> ( -> FirstName varchar(200), -> SecondName varchar(255) unicode -> );
将记录插入表中。
mysql> insert into LengthAndCharLengthDemo values('John','Ritter');
在select的帮助下显示表中的所有记录。
mysql> select *from LengthAndCharLengthDemo;
这是输出。
+-----------+------------+ | FirstName | SecondName | +-----------+------------+ | John | Ritter | +-----------+------------+ 1 row in set (0.00 sec)
让我们得到长度。
mysql> select char_length(FirstName),length(Secondname) from LengthAndCharLengthDemo -> where FirstName='John' and Secondname='John';
这是输出。
+------------------------+--------------------+ | char_length(FirstName) | length(SecondName) | +------------------------+--------------------+ | 4 | 8 | +------------------------+--------------------+ 1 row in set (0.70 sec)
在上面的输出中,您可以看到length()
函数由于unicode而返回8个字节,而char_length()仅返回4个字节。