如何编写一个 MySQL 查询,为多个输入显示一个值?
为此,请使用 BETWEEN 关键字。我们首先创建一个表 -
mysql> create table DemoTable1537 -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(20) -> ); Query OK, 0 rows affected (0.72 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable1537(StudentName) values('Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1537(StudentName) values('Bob'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1537(StudentName) values('Sam'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1537(StudentName) values('Mike'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1537(StudentName) values('David'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1537(StudentName) values('John'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1537(StudentName) values('Carol'); Query OK, 1 row affected (0.12 sec)
使用 select 语句从表中显示所有记录 -
mysql> select * from DemoTable1537;
这将产生以下输出 -
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 1 | Chris | | 2 | Bob | | 3 | Sam | | 4 | Mike | | 5 | David | | 6 | John | | 7 | Carol | +-----------+-------------+ 7 rows in set (0.00 sec)
以下是为多个输入显示值的查询 -
mysql> select StudentName from DemoTable1537 where StudentId between 3 and 6;
这将产生以下输出 -
+-------------+ | StudentName | +-------------+ | Sam | | Mike | | David | | John | +-------------+ 4 rows in set (0.00 sec)
广告