如何找出 MySQL 中某個特定分數以上學生的記錄?


使用 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)

更新時間: 2020 年 6 月 30 日

109 次瀏覽

開啟你的職業生涯

完成課程,獲得認證

開始吧
廣告