在本教學中,您將了解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);
我們來看看下面的內容:
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
上面查詢語句是如何工作的?
ucs2
字元集,這是UCS-2 Unicode
,指定一個字元儲存為2
個位元組。CHAR_LENGTH
和LENGTH
函式來獲取@s
字串的長度(以位元組為單位)。因為@s
字串以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 |
+------------+-----------------+
1 row in set
我們使用latin1
字元集來儲存@s
字串。 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
假設我們有一個posts
表,用於儲存具有四列:postid
, title
, excerpt
和 content
(為了演示目的,所以posts
表盡可能簡單)。
首先,我們使用CREATE TABLE語句建立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;
其次,通過使用INSERT語句將一些部落格貼文插入到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
在SELECT語句中,我們使用IF
函式來檢查摘錄列的長度是否大於20
,並使用CONCAT語句將excerpt
列的值與省略號(...
)連線起來,否則只是獲取全部摘錄(excerpt
)內容。
在本教學中,我們向您展示了如何使用MySQL字串長度函式來獲取字串的長度(以位元組為單位)。