MySQL 中查詢字元少於 X 個的查詢結果?


可以使用 CHAR_LENGTH() 以及 WHERE 子句。我們首先建立一個表 -

mysql> create table DemoTable
   -> (
   -> FullName varchar(50)
   -> );
Query OK, 0 rows affected (1.75 sec)

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

mysql> insert into DemoTable values('Chris Brown');
Query OK, 1 row affected (0.40 sec)
mysql> insert into DemoTable values('David Miller');
Query OK, 1 row affected (0.91 sec)
mysql> insert into DemoTable values('Robert Miller');
Query OK, 1 row affected (0.26 sec)
mysql> insert into DemoTable values('John Smith');
Query OK, 1 row affected (0.89 sec)

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

mysql> select *from DemoTable;

這將產生以下輸出 -

+---------------+
| FullName      |
+---------------+
| Chris Brown   |
| David Miller  |
| Robert Miller |
| John Smith    |
+---------------+
4 rows in set (0.00 sec)

以下是獲取 MySQL 中字元少於 X 個的記錄的查詢 -

mysql> select *from DemoTable where char_length(FullName) < 12;

這將產生以下輸出 -

+-------------+
| FullName    |
+-------------+
| Chris Brown |
| John Smith  |
+-------------+
2 rows in set (0.00 sec)

更新於: 2019 年 12 月 17 日

135 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始
廣告