在 MySQL 中提取具有 marks1 和 marks2 記錄的學生的最大個人分數?
為此,請將 MAX() 與 GROUP BY 子句配合使用。我們首先建立一個表 −
mysql> create table DemoTable -> ( -> StudentEmailId varchar(20), -> Marks1 int, -> Marks2 int -> ); Query OK, 0 rows affected (0.90 sec)
使用 insert 命令在表中插入一些記錄 −
mysql> insert into DemoTable values('John@gmail.com',45,32);
Query OK, 1 row affected (0.30 sec)
mysql> insert into DemoTable values('John@gmail.com',32,45);
Query OK, 1 row affected (0.34 sec)
mysql> insert into DemoTable values('Carol@gmail.com',32,45);
Query OK, 1 row affected (1.64 sec)
mysql> insert into DemoTable values('David@gmail.com',45,32);
Query OK, 1 row affected (0.18 sec)使用 select 語句顯示錶中的所有記錄 −
mysql> select *from DemoTable ;
這將產生以下輸出 −
+-----------------+--------+--------+ | StudentEmailId | Marks1 | Marks2 | +-----------------+--------+--------+ | John@gmail.com | 45 | 32 | | John@gmail.com | 32 | 45 | | Carol@gmail.com | 32 | 45 | | David@gmail.com | 45 | 32 | +-----------------+--------+--------+ 4 rows in set (0.00 sec)
這是查詢具有 marks1 和 marks2 記錄的學生的最大個人分數 −
mysql> select StudentEmailId,max(Marks1),max(Marks2) from DemoTable -> group by StudentEmailId;
這將產生以下輸出 −
+-----------------+-------------+-------------+ | StudentEmailId | max(Marks1) | max(Marks2) | +-----------------+-------------+-------------+ | John@gmail.com | 45 | 45 | | Carol@gmail.com | 32 | 45 | | David@gmail.com | 45 | 32 | +-----------------+-------------+-------------+ 3 rows in set (0.00 sec)
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP