怎么新建MySQL数据库

发布时间:2022-06-23 作者:admin
阅读:295
在实际应用中,我们有时候会遇到“MariaDB中的MAX()函数用于做什么,可以怎样应用”这样的问题,我们该怎样来处理呢?下文给大家介绍了解决方法,希望这篇“MariaDB中的MAX()函数用于做什么,可以怎样应用”文章能帮助大家解决问题。



MariaDB MAX()函数用于检索表达式的最大值。

语法:

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

数据准备

"testdb"数据库中创建一个"students"表,并插入一些数据。参考以下创建语句 -

USE testdb;
DROP TABLE students;
-- 创建新表
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 )
);
-- 插入数据
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()函数与单表达式

示例:

查询Student表的最低分数。参考以下查询语句 -

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

执行上面查询语句,得到以下结果 -

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

2. MAX()函数与GROUP BY子句

可以使用MAX函数的GROUP BY子句来分组。

再插入一条重复的数据 -

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

当前数据库中的记录如下 -

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;

执行上面查询语句,得到以下结果 -

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中的MAX()函数用于做什么,可以怎样应用”就介绍到这了,如果大家觉得不错可以参考了解看看,如果想要了解更多,欢迎关注群英网络,小编每天都会为大家更新不同的知识。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

二维码-群英

长按识别二维码并关注微信

更方便到期提醒、手机管理

7*24 全天候服务

售前 400-678-4567

售后 0668-2555666

售后 400 678 4567

信息安全 0668-2555 118

域名空间 3004329145