利用兩次 LIKE 子句從包含重複的 FirstName 和 LastName 的表中顯示具體姓名
我們先建立一個表 -
mysql> create table DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(30) ); Query OK, 0 rows affected (0.70 sec)
使用 insert 命令在表中插入一些記錄 -
mysql> insert into DemoTable(StudentName) values('John Smith');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable(StudentName) values('David Miller');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable(StudentName) values('Carol Taylor');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable(StudentName) values('John Doe');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable(StudentName) values('Chris Brown');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable(StudentName) values('Adam Doe');
Query OK, 1 row affected (0.11 sec)使用 select 語句從表中顯示所有記錄 -
mysql> select *from DemoTable;
這將生成以下輸出 -
+-----------+---------------+ | StudentId | StudentName | +-----------+---------------+ | 1 | John Smith | | 2 | David Miller | | 3 | Carol Taylor | | 4 | John Doe | | 5 | Chris Brown | | 6 | Adam Doe | +-----------+---------------+ 6 rows in set (0.00 sec)
以下是如何從表中選擇名字為“John”、姓氏為“Doe”的記錄的查詢 -
mysql> select *from DemoTable where StudentName LIKE '%John%' and StudentName LIKE '%Doe%';
這將生成以下輸出 -
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 4 | John Doe | +-----------+-------------+ 1 row in set (0.00 sec)
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP