同時搜尋 2 個欄位從 MySQL 中的一個表中提取一個特定的名和姓。


為此,您可以將 LIKE 運算子與 AND 一起使用。讓我們先建立一個表 -

mysql> create table DemoTable
(
   EmployeeFirstName varchar(50),
   EmployeeLastName varchar(50)
);
Query OK, 0 rows affected (0.64 sec)

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

mysql> insert into DemoTable values('John','Smith');
Query OK, 1 row affected (0.61 sec)
mysql> insert into DemoTable values('David','Miller');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('John','Doe');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable values('Adam','Smith');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable values('Carol','Taylor');
Query OK, 1 row affected (0.08 sec)

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

mysql> select *from DemoTable;

將生成以下輸出 -

+-------------------+------------------+
| EmployeeFirstName | EmployeeLastName |
+-------------------+------------------+
| John              | Smith            |
| David             | Miller           |
| John              | Doe              |
| Adam              | Smith            |
| Carol             | Taylor           |
+-------------------+------------------+
5 rows in set (0.00 sec)

以下是同時搜尋 2 個欄位以提取以“Joh”開頭的 FirstName 和以“Sm”開頭的 LastName 的記錄的查詢 -

mysql> select *from DemoTable
where EmployeeFirstName LIKE 'Joh%'
and EmployeeLastName LIKE 'Sm%';

將生成以下輸出 -

+-------------------+------------------+
| EmployeeFirstName | EmployeeLastName |
+-------------------+------------------+
| John              | Smith            |
+-------------------+------------------+
1 row in set (0.00 sec)

更新於: 03-10-2019

88 次瀏覽

開啟你的 職業 生涯

修完全部課程來獲得認證

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