使用LIKE子句两次显示重复的名字和姓氏的表中的特定名称
让我们首先创建一个表-
mysql> create table DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(30) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(StudentName) values('John Smith'); mysql> insert into DemoTable(StudentName) values('David Miller'); mysql> insert into DemoTable(StudentName) values('Carol Taylor'); mysql> insert into DemoTable(StudentName) values('John Doe'); mysql> insert into DemoTable(StudentName) values('Chris Brown'); mysql> insert into DemoTable(StudentName) values('Adam Doe');
使用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)