僅在 MySQL 中包含兩個連字元時檢索?


為此,請使用 LIKE 運算子。我們首先建立一個表

mysql> create table DemoTable
   (
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   Password varchar(100)
   );
Query OK, 0 rows affected (1.27 sec)

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

mysql> insert into DemoTable(Password) values('John@--123');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable(Password) values('---Carol234');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable(Password) values('--David987');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable(Password) values('Mike----53443');
Query OK, 1 row affected (0.30 sec)

使用 select 語句從表中顯示所有記錄

mysql> select *from DemoTable;

輸出

+----+---------------+
| Id | Password      |
+----+---------------+
| 1  | John@--123    |
| 2  | ---Carol234   |
| 3  | --David987    |
| 4  | Mike----53443 |
+----+---------------+
4 rows in set (0.00 sec)

以下是僅在 MySQL 中包含兩個連字元時進行檢索的查詢 -

mysql> select *from DemoTable where Password like '%--%' and password not like '%---%';

輸出

+----+------------+
| Id | Password   |
+----+------------+
| 1  | John@--123 |
| 3  | --David987 |
+----+------------+
2 rows in set (0.06 sec)

更新於: 2019-07-30

478 次瀏覽

開啟你 職業生涯

完成課程,獲得認證

立即開始
廣告
© . All rights reserved.