MySQL函數length 和 char_length的區別

length 和 char_length都是計算字段的字符長度,可是計算的方式不一樣mysql

length :一個漢字算三個字符,一個數字、一個符號和一個字母算一個字符;sql

char_length:一個漢字、一個數字、一個符號和一個字母都算一個字符,也就是說以個數來計算;shell

看下面測試結果測試

"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)
相關文章
相關標籤/搜索