MySQL字符串长度
转载⾃
在本教程中,您将了解MySQL字符串长度函数,使您可以以字节为单位获取字符串的长度。
MySQL⽀持各种,如latin1,utf8等。可以使⽤SHOW CHARACTER SET语句来获取MySQL数据库服务器⽀持的所有字符集。
mysql> SHOW CHARACTER SET;
+----------+---------------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+---------------------------------+---------------------+--------+
| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
| dec8 | DEC West European | dec8_swedish_ci | 1 |
| cp850 | DOS West European | cp850_general_ci | 1 |
| hp8 | HP West European | hp8_english_ci | 1 |
| koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
| latin1 | cp1252 West European | latin1_swedish_ci | 1 |
| latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 |
| swe7 | 7bit Swedish | swe7_swedish_ci | 1 |
| ascii | US ASCII | ascii_general_ci | 1 |
| ujis | EUC-JP Japanese | ujis_japanese_ci | 3 |
| sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 |
| hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
| tis620 | TIS620 Thai | tis620_thai_ci | 1 |
| euckr | EUC-KR Korean | euckr_korean_ci | 2 |
| koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 |
| gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 |
| greek | ISO 8859-7 Greek | greek_general_ci | 1 |
| cp1250 | Windows Central European | cp1250_general_ci | 1 |
| gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 |
| latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 |
| armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 |
| utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
| ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 |
| cp866 | DOS Russian | cp866_general_ci | 1 |
| keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 |
| macce | Mac Central European | macce_general_ci | 1 |
| macroman | Mac West European | macroman_general_ci | 1 |
| cp852 | DOS Central European | cp852_general_ci | 1 |
| latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 |
| utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci | 4 |
| cp1251 | Windows Cyrillic | cp1251_general_ci | 1 |
| utf16 | UTF-16 Unicode | utf16_general_ci | 4 |
| utf16le | UTF-16LE Unicode | utf16le_general_ci | 4 |
| cp1256 | Windows Arabic | cp1256_general_ci | 1 |
| cp1257 | Windows Baltic | cp1257_general_ci | 1 |
| utf32 | UTF-32 Unicode | utf32_general_ci | 4 |
| binary | Binary pseudo charset | binary | 1 |
| geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 |
| cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 |
| eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 |
| gb18030 | China National Standard GB18030 | gb18030_chinese_ci | 4 |
+----------+---------------------------------+---------------------+--------+
41 rows in set
Maxlen列存储字符集的字节数。在MySQL中,⼀个字符串可以是任何字符集。 如果⼀个字符串包含1个字节的字符,则其字符长度和以字节为单位测量的长度相等。 但是,如果字符串包含多字节字符,则其字节长度通常⼤于字符长度。
要获取以字节为单位的字符串长度,请使⽤LENGTH函数,如下所⽰:
LENGTH(str);
可以使⽤CHAR_LENGTH函数获取字符串的长度,以字符为单位计算长度,如下所⽰:
CHAR_LENGTH(str);
LENGTH和CHAR_LENGTH函数的⽰例
我们来看看下⾯的内容:
mysql> SET @s = CONVERT('MySQL String Length' USING ucs2);
SELECT CHAR_LENGTH(@s), LENGTH(@s);
Query OK, 0 rows affected
+-----------------+------------+
| CHAR_LENGTH(@s) | LENGTH(@s) |
+-----------------+------------+
| 19 | 38 |
+-----------------+------------+
1 row in set
上⾯查询语句是如何⼯作的?
⾸先,我们将MySQL字符串长度字符串转换为ucs2字符集,这是UCS-2 Unicode,指定⼀个字符存储为2个字节。
其次,分别使⽤CHAR_LENGTH和LENGTH函数来获取字符串的长度(以字节为单位)。因为字符串以2个字节来存储每个字符,所以其字符长度为19,⽽字节长度为38 。
以下语句演⽰了LENGTH和CHAR_LENGTH函数如何使⽤1个字节来存储每个字符:
SET @s = CONVERT('MySQL string length' USING latin1);
SELECT LENGTH(@s), CHAR_LENGTH(@s);
执⾏上⾯查询语句,得到以下结果 -
+------------+-----------------+
| LENGTH(@s) | CHAR_LENGTH(@s) |
+------------+-----------------+
| 19 | 19 |
简单的mysql语句+------------+-----------------+
1 row in set
我们使⽤latin1字符集来存储字符串。 latin1字符集是使⽤1个字节来存储每个字符的; 因此,其字节长度和字符长度相等。
请注意,某些字符集可以更改字符的存储字节数,例如,utf8字符集:
SET @s = CONVERT('MySQL String Length' USING utf8);
SELECT CHAR_LENGTH(@s), LENGTH(@s);
执⾏上⾯查询语句,得到以下结果 -
mysql> SET @s = CONVERT('MySQL String Length' USING utf8);
SELECT CHAR_LENGTH(@s), LENGTH(@s);
Query OK, 0 rows affected
+-----------------+------------+
| CHAR_LENGTH(@s) | LENGTH(@s) |
+-----------------+------------+
| 19 | 19 |
+-----------------+------------+
1 row in set
CHAR_LENGTH和LENGTH返回相同的结果。 但是,如果⼀个字符串有特殊字符,结果是不同的。 请参阅以下⽰例:
mysql> SET @s = CONVERT('á' USING utf8);
SELECT CHAR_LENGTH(@s), LENGTH(@s);
Query OK, 0 rows affected
+-----------------+------------+
| CHAR_LENGTH(@s) | LENGTH(@s) |
+-----------------+------------+
| 1 | 2 |
+-----------------+------------+
1 row in set
中⽂字符串存储多少位?看看下⾯查询就知道了 -
mysql> SET @s = CONVERT('易百教程' USING utf8);
SELECT CHAR_LENGTH(@s), LENGTH(@s);
Query OK, 0 rows affected
+-----------------+------------+
| CHAR_LENGTH(@s) | LENGTH(@s) |
+-----------------+------------+
| 4 | 12 |
+-----------------+------------+
1 row in set
⼀个应⽤程序的MySQL字符串长度函数
假设我们有⼀个posts表,⽤于存储具有四列:postid, title, excerpt 和 content(为了演⽰⽬的,所以posts表尽可能简单)。⾸先,我们使⽤语句创建posts表:
USE testdb;
CREATE TABLE posts(
postid int auto_increment primary key,
title varchar(255) NOT NULL,
excerpt varchar(255) NOT NULL,
content text,
pubdate datetime
)Engine=InnoDB;
其次,通过使⽤语句将⼀些博客帖⼦插⼊到posts表中:
INSERT INTO posts(title,excerpt,content)
VALUES('MySQL Length','MySQL string length function tutorial','dummy'),
('Second blog post','Second blog post','dummy');
我们可以使⽤CHAR_LENGTH函数来检查是否有超过20个字符,如果超过了,则附加省略号(...),如下查询语句:
SELECT postid,
title,
IF(CHAR_LENGTH(excerpt) > 20,
CONCAT(LEFT(excerpt,20), '...'),
excerpt) summary
FROM posts;
执⾏上⾯查询语句,得到以下结果 -
+--------+------------------+-------------------------+
| postid | title | summary |
+--------+------------------+-------------------------+
| 1 | MySQL Length | MySQL string length ... |
| 2 | Second blog post | Second blog post |
+--------+------------------+-------------------------+
2 rows in set
在语句中,我们使⽤IF函数来检查摘录列的长度是否⼤于20,并使⽤CONCAT语句将excerpt列的值与省略号(...)连接起来,否则只是获取全部摘录(excerpt)内容。
在本教程中,我们向您展⽰了如何使⽤MySQL字符串长度函数来获取字符串的长度(以字节为单位)。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论