在MySQL中查找超过特定分数的学生记录?

mysqlmysqli database更新于 2023/11/26 17:44:00

通过WHERE设置,获取超过特定分数的学生记录。我们首先创建一个表。 −

mysql> create table DemoTable
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> StudentName varchar(100),
   -> StudentScore int
   -> );
Query OK, 0 rows affected (1.65 sec

使用 insert 命令在表中插入一些记录 −

mysql> insert into DemoTable(StudentName,StudentScore) values('John',43);
Query OK, 1 row affected (0.81 sec)

mysql> insert into DemoTable(StudentName,StudentScore) values('Sam',48);
Query OK, 1 row affected (0.12 sec)

mysql> insert into DemoTable(StudentName,StudentScore) values('Chris',33);
Query OK, 1 row affected (1.50 sec)

mysql> insert into DemoTable(StudentName,StudentScore) values('Robert',89);
Query OK, 1 row affected (0.27 sec)

使用 select 语句显示表中的所有记录 −

mysql> select *from DemoTable;

输出

这将产生以下输出 −

+----+-------------+--------------+
| Id | StudentName | StudentScore |
+----+-------------+--------------+
|  1 | John        | 43           |
|  2 | Sam         | 48           |
|  3 | Chris       | 33           |
|  4 | Robert      | 89           |
+----+-------------+--------------+
4 rows in set (0.00 sec)

下面是获取分数超过45分的学生记录的查询 −

mysql> select StudentName from DemoTable where StudentScore > 45;

输出

这将产生以下输出 −

+-------------+
| StudentName |
+-------------+
| Sam         |
| Robert      |
+-------------+
2 rows in set (0.00 sec)

相关文章