MySQL函数length 和 char_length的区别

length 和 char_length都是计算字段的字符长度,但是计算的方式不同

length :一个汉字算三个字符,一个数字、一个符号和一个字母算一个字符;

char_length:一个汉字、一个数字、一个符号和一个字母都算一个字符,也就是说以个数来计算;

看下面测试结果

"root@localhost:mysql.sock  [pxs]>show global variables like '%character%';
+--------------------------+----------------------------------+
| Variable_name            | Value                            |
+--------------------------+----------------------------------+
| character_set_client     | utf8                             |
| character_set_connection | utf8                             |
| character_set_database   | utf8                             |
| character_set_filesystem | binary                           |
| character_set_results    | utf8                             |
| character_set_server     | utf8                             |
| character_set_system     | utf8                             |
| character_sets_dir       | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.02 sec)

"root@localhost:mysql.sock  [pxs]>select * from pxs;
+-----------+
| test      |
+-----------+
| 123       |
| 逗逼啊    |
| DBA       |
| ./&       |
| dba       |
+-----------+
5 rows in set (0.00 sec)

"root@localhost:mysql.sock  [pxs]>select test,length(test) as length from pxs;
+-----------+--------+
| test      | length |
+-----------+--------+
| 123       |      3 |
| 逗逼啊    |      9 |
| DBA       |      3 |
| ./&       |      3 |
| dba       |      3 |
+-----------+--------+
5 rows in set (0.00 sec)

"root@localhost:mysql.sock  [pxs]>select test,char_length(test) as char_length from pxs;
+-----------+-------------+
| test      | char_length |
+-----------+-------------+
| 123       |           3 |
| 逗逼啊    |           3 |
| DBA       |           3 |
| ./&       |           3 |
| dba       |           3 |
+-----------+-------------+
5 rows in set (0.00 sec)



你可能感兴趣的:(MySQL函数length 和 char_length的区别)