如何在 MySQL CREATE TABLE 查詢中使用 CHAR_LENGTH()?


在建立表時使用 CHAR_LENGTH(yourColumnName)。我們先看一個示例並建立一個表 -

mysql> create table DemoTable
   (
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   Title varchar(200),
   `Number_of_characters` int as (char_length(Title))
   );
Query OK, 0 rows affected (0.18 sec)

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

mysql> insert into DemoTable(Title) values('Introduction To MySQL');
Query OK, 1 row affected (0.06 sec)

mysql> insert into DemoTable(Title) values('Introduction To Java');
Query OK, 1 row affected (0.06 sec)

mysql> insert into DemoTable(Title) values('Introduction To MongoDB');
Query OK, 1 row affected (0.04 sec)

使用 select 語句顯示錶中的所有記錄 -

mysql> select *from DemoTable;

將生成以下輸出 -

+----+-------------------------+----------------------+
| Id | Title                   | Number_of_characters |
+----+-------------------------+----------------------+
| 1  | Introduction To MySQL   | 21                   |
| 2  | Introduction To Java    | 20                   |
| 3  | Introduction To MongoDB | 23                   |
+----+-------------------------+----------------------+
3 rows in set (0.00 sec)

更新於: 30-Jul-2019

82 次瀏覽

開啟你的 職業

完成課程獲得認證

開始吧
廣告
© . All rights reserved.