用於獲取列中所有值字元長度的 MySQL 查詢?


若要獲取字元長度,請使用 CHAR_LENGTH() 方法。我們先建立一個表 −

mysql> create table DemoTable
   -> (
   -> Name varchar(100)
   -> );
Query OK, 0 rows affected (1.04 sec)

使用插入命令在表中插入一些記錄 −

mysql> insert into DemoTable values('John');
Query OK, 1 row affected (0.13 sec)

mysql> insert into DemoTable values('Robert');
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable values('Bob');
Query OK, 1 row affected (0.20 sec)

mysql> insert into DemoTable values('David');
Query OK, 1 row affected (0.15 sec)

使用選擇語句顯示錶中的所有記錄 −

mysql> select *from DemoTable;

輸出

這將產生以下輸出 −

+--------+
| Name   |
+--------+
| John   |
| Robert |
| Bob    |
| David  |
+--------+
4 rows in set (0.00 sec)

獲取字元長度 −

mysql> select char_length(Name) from DemoTable;

輸出

這將產生以下輸出 −

+-------------------+
| char_length(Name) |
+-------------------+
| 4                 |
| 6                 |
| 3                 |
| 5                 |
+-------------------+
4 rows in set (0.00 sec)

更新於: 30-6 月-2020

190 瀏覽次數

開啟你的職業生涯

完成課程即可獲得認證

開始學習
廣告
© . All rights reserved.