使用 MySQL IN() 根据 LastName 提取记录
让我们首先创建一个表 −
mysql> create table DemoTable ( FirstName varchar(100), LastName varchar(100) ); Query OK, 0 rows affected (0.83 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values('Adam','Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('John','Doe'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('John','Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Chris','Brown'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('Robert','Brown'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('David','Miller'); Query OK, 1 row affected (0.11 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
这将生成以下输出 −
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | Adam | Smith | | John | Doe | | John | Smith | | Chris | Brown | | Robert | Brown | | David | Miller | +-----------+----------+ 6 rows in set (0.00 sec)
以下是使用 MySQL IN() 根据 LastName 提取记录的查询 −
mysql> select *from DemoTable where LastName IN('Smith','Brown','Miller');
这将生成以下输出 −
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | Adam | Smith | | John | Smith | | Chris | Brown | | Robert | Brown | | David | Miller | +-----------+----------+ 5 rows in set (0.00 sec)
广告