鍍金池/ 教程/ 數(shù)據(jù)庫/ MariaDB Max()函數(shù)
MariaDB比較運算符
MariaDB查詢數(shù)據(jù)
MariaDB Like子句
MariaDB Sum()函數(shù)
MariaDB過程
MariaDB限制返回記錄
MariaDB Count()函數(shù)
MariaDB更新數(shù)據(jù)
MariaDB導出數(shù)據(jù)
MariaDB Intersect運算符
MariaDB函數(shù)
MariaDB Min()函數(shù)
MariaDB創(chuàng)建數(shù)據(jù)庫
MariaDB Avg()函數(shù)
MariaDB刪除數(shù)據(jù)
MariaDB條件
MariaDB功能特點
MariaDB創(chuàng)建表
MariaDB左外連接
MariaDB Union運算符
MariaDB安裝
MariaDB選擇數(shù)據(jù)庫
MariaDB Max()函數(shù)
MariaDB Where子句
MariaDB右外連接
MariaDB內(nèi)連接
MariaDB截斷表
MariaDB Order By子句
MariaDB教程
MariaDB正則表達式
MariaDB Union All運算符
MariaDB Distinct子句
MariaDB修改表
MariaDB刪除表
MariaDB插入數(shù)據(jù)
MariaDB From子句
MariaDB刪除數(shù)據(jù)庫
MariaDB簡介
MariaDB數(shù)據(jù)類型

MariaDB Max()函數(shù)

MariaDB MAX()函數(shù)用于檢索表達式的最大值。

語法:

SELECT MAX(aggregate_expression)  
FROM tables  
[WHERE conditions];

數(shù)據(jù)準備

"testdb"數(shù)據(jù)庫中創(chuàng)建一個"students"表,并插入一些數(shù)據(jù)。參考以下創(chuàng)建語句 -

USE testdb;
DROP TABLE students;
-- 創(chuàng)建新表
CREATE TABLE students(  
    student_id INT NOT NULL AUTO_INCREMENT,  
    student_name VARCHAR(100) NOT NULL,  
    student_address VARCHAR(40) default NULL, 
    admission_date DATE,
    score float(4, 1) default NULL, 
    PRIMARY KEY ( student_id )
);
-- 插入數(shù)據(jù)
INSERT INTO students  
(student_id, student_name, student_address,score, admission_date)  
VALUES(1,'Maxsu','Haikou', 99.5,'2017-01-07 00:00:00');

INSERT INTO students  
(student_id, student_name, student_address, score, admission_date)  
VALUES  
(2,'Crurry','Beijing',86,'2016-05-07 00:00:00'),
(3,'JMaster','Beijing',91,'2016-05-07 00:00:00'),  
(4,'Mahesh','Guangzhou',78,'2016-06-07 00:00:00'),  
(5,'Kobe','Shanghai',89,'2016-02-07 00:00:00'),  
(6,'Blaba','Shengzhen',100,'2016-08-07 00:00:00');

1. MAX()函數(shù)與單表達式

示例:

查詢Student表的最低分數(shù)。參考以下查詢語句 -

SELECT MAX(Score) AS "Greatest Score" FROM students;

執(zhí)行上面查詢語句,得到以下結(jié)果 -

MariaDB [testdb]> SELECT MAX(Score) AS "Greatest Score" FROM students;
+----------------+
| Greatest Score |
+----------------+
|          100.0 |
+----------------+
1 row in set (0.03 sec)

2. MAX()函數(shù)與GROUP BY子句

可以使用MAX函數(shù)的GROUP BY子句來分組。

再插入一條重復的數(shù)據(jù) -

INSERT INTO students  
(student_name, student_address,score, admission_date)  
VALUES('Maxsu','Haikou', 91,'2017-11-07 00:00:00');

當前數(shù)據(jù)庫中的記錄如下 -

MariaDB [testdb]> select * from students;
+------------+--------------+-----------------+----------------+-------+
| student_id | student_name | student_address | admission_date | score |
+------------+--------------+-----------------+----------------+-------+
|          1 | Maxsu        | Haikou          | 2017-01-07     |  99.5 |
|          2 | Crurry       | Beijing         | 2016-05-07     |  86.0 |
|          3 | JMaster      | Beijing         | 2016-05-07     |  91.0 |
|          4 | Mahesh       | Guangzhou       | 2016-06-07     |  78.0 |
|          5 | Kobe         | Shanghai        | 2016-02-07     |  89.0 |
|          6 | Blaba        | Shengzhen       | 2016-08-07     | 100.0 |
|          7 | Maxsu        | Haikou          | 2017-11-07     |  91.0 |
+------------+--------------+-----------------+----------------+-------+
7 rows in set (0.07 sec)

示例:

SELECT student_name, MAX(score) AS "Lowest Score"  
FROM Students  
where student_id < 10  
GROUP BY student_name;

執(zhí)行上面查詢語句,得到以下結(jié)果 -

MariaDB [testdb]> SELECT student_name, MAX(score) AS "Lowest Score"
    -> FROM Students
    -> where student_id < 10
    -> GROUP BY student_name;
+--------------+--------------+
| student_name | Lowest Score |
+--------------+--------------+
| Blaba        |        100.0 |
| Crurry       |         86.0 |
| JMaster      |         91.0 |
| Kobe         |         89.0 |
| Mahesh       |         78.0 |
| Maxsu        |         99.5 |
+--------------+--------------+
6 rows in set (0.04 sec)

上一篇:MariaDB教程下一篇:MariaDB內(nèi)連接