如何在 MySQL 中檢視列中的任何字串是否包含給定字串?


為此,需要使用 CONCAT() 與 LIKE 運算子。我們先建立一個表 –

mysql> create table DemoTable
(
   Name varchar(40)
);
Query OK, 0 rows affected (0.56 sec)

使用 insert 命令向表中插入一些記錄 –

mysql> insert into DemoTable values('John');
Query OK, 1 row affected (0.33 sec)
mysql> insert into DemoTable values('Adam');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values('Bob');
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable values('Johnson');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('David');
Query OK, 1 row affected (0.12 sec)

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

mysql> select *from DemoTable;

這將會產生以下輸出 –

+---------+
| Name    |
+---------+
| John    |
| Adam    |
| Bob     |
| Johnson |
| David   |
+---------+
5 rows in set (0.00 sec)

以下查詢用於檢視列中的任何字串是否包含給定字串 –

mysql> select *from DemoTable where Name like "%Joh%" OR "Joh" LIKE CONCAT("%", Name, "%");

這將會產生以下輸出 –

+---------+
| Name    |
+---------+
| John    |
| Johnson |
+---------+
2 rows in set (0.00 sec)

更新於: 09-Oct-2019

1K+ 瀏覽量

開啟你的職業生涯 生涯

透過完成課程獲得職業資格認證

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