如何透過特定字串搜尋 MySQL 表?


對於完全匹配,請使用等於運算子 −

select *from yourTableName where yourColumnName=yourValue;

讓我們先建立一個表 −

mysql> create table DemoTable
-> (
-> FirstName varchar(100),
-> LastName varchar(100)
-> );
Query OK, 0 rows affected (0.70 sec

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

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

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

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

mysql> insert into DemoTable values('Carol','Taylor');
Query OK, 1 row affected (0.29 sec)

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

mysql> select *from DemoTable;

輸出

這將產生以下輸出 −

+-----------+----------+
| FirstName | LastName |
+-----------+----------+
| John      | Smith    |
| John      | Doe      |
| Chris     | Brown    |
| Carol     | Taylor   |
+-----------+----------+
4 rows in set (0.00 sec)

以下是用於透過特定字串搜尋 MySQL 表的查詢 −

mysql> select *from DemoTable where LastName='Doe';

輸出

這將產生以下輸出 −

+-----------+----------+
| FirstName | LastName |
+-----------+----------+
| John      | Doe      |
+-----------+----------+
1 row in set (0.00 sec)

更新於: 30-Jun-2020

2K+ 次瀏覽

開啟你的職業生涯

透過完成課程獲取認證

開始
廣告
© . All rights reserved.