轻松上手,快乐学习!

MySQL 教程

MySQL 首页MySQL 介绍MySQL 安装MySQL 实例库下载MySQL 实例库导入MySQL SELECTMySQL DISTINCTMySQL ORDER BYMySQL WHEREMySQL ANDMySQL ORMySQL INMySQL BETWEENMySQL LIKEMySQL LIMITMySQL IS NULLMySQL 别名MySQL JOINSMySQL INNER JOINMySQL LEFT JOINMySQL RIGHT JOINMySQL CROSS JOINMySQL SELF JOINMySQL GROUP BYMySQL HAVINGMySQL ROLLUPMySQL 子查询MySQL 派生表MySQL EXISTSMySQL CTEMySQL 递归 CTEMySQL UNIONMySQL INTERSECTMySQL MINUSMySQL INSERTMySQL INSERT INTO SELECTMySQL INSERT IGNOREMySQL UPDATEMySQL UPDATE JOINMySQL DELETEMySQL ON DELETE CASCADEMySQL DELETE JOINMySQL REPLACEMySQL PREPAREMySQL 事务MySQL 表锁定MySQL USEMySQL 数据库管理MySQL CREATE DATABASEMySQL DROP DATABASEMySQL 存储引擎MySQL CREATE TABLEMySQL 序列MySQL ALTER TABLEMySQL RENAME TABLEMySQL DROP COLUMNMySQL ADD COLUMNMySQL DROP TABLEMySQL 临时表MySQL TRUNCATE TABLEMySQL 数据类型MySQL NOT NULLMySQL Primary KeyMySQL Foreign KeyMySQL UNIQUEMySQL CHECKMySQL 字符集MySQL 排序规则MySQL 导入 CSVMySQL 导出 CSVMySQL 自然排序MySQL 基础MySQL NOT INMySQL Storage Engines

MySQL 存储过程

MySQL 存储过程介绍MySQL 存储过程实例MySQL 存储过程变量MySQL 存储过程参数MySQL 存储过程返回多值MySQL IF 语句MySQL CASE 语句MySQL IF CASE 选择MySQL 存储过程循环MySQL 存储过程游标MySQL 存储过程列表MySQL 存储过程异常处理MySQL SIGNAL 和 RESIGNALMySQL 存储函数MySql 自定义存储

MySQL 视图

MySQL 视图教程SQL 视图介绍MySQL 视图介绍MySQL 创建视图MySQL 可更新视图WITH CHECK OPTIONLOCAL&CASCADEDMySQL 视图管理

MySQL 触发器

MySQL 触发器SQL 触发器MySQL 触发器介绍MySQL 触发器的创建MySQL 创建多个触发器MySQL 触发器管理MySQL 计划事件MySQL事件修改

MySQL 索引

MySQL 索引MySQL 索引创建MySQL 索引删除MySQL 显示索引MySQL 唯一索引MySQL 前缀索引MySQL 隐形索引MySQL 降序索引MySQL 复合索引MySQL 聚集索引MySQL 索引基数MySQL USE INDEXMySQL 强制索引

MySQL 管理

MySQL 管理MySQL访问控制系统MySQL 用户创建MySQL 用户密码MySQL 权限授予MySQL 权限撤销MySQL 角色MySQL 删除用户MySQL 表维护mysqldump 备份工具MySQL 数据库列表MySQL 表列表MySQL 表字段列表MySQL 用户列表MySQL 进程列表MySQL 列生成MySQL 表自连接MySQL 恢复

MySQL 全文搜索

MySQL 全文搜索MySQL 全文搜索介绍FULLTEXT索引MySQL 自然语言全文搜索MySQL 布尔全文搜索MySQL查询扩展MySQL ngram

MySQL 高级

MySQL 函数MySQL 窗口函数

MySQL 技巧

MySQL 查找重复数据MySQL 删除重复数据MySQL UUIDMySQL 表的复制MySQL 复制库MySQL 变量MySQL SELECT INTO 变量MySQL 表的存储引擎MySQL 使用正则查询MySQL 添加序号MySQL 随机查询MySQL 查询第 N 高记录MySQL 重置自增值MySQL VS MariaDBMySQL 间隔值MySQL 获取当天日期MySQL NULL 映射MySQL 注释理解MySQL EXPLAINMySQL 技巧介绍MySQL COUNTMySQL 数据分层MySQL 两表比较Mysql Like 优化

MySQL 窗口函数

MySQL CUME_DIST() 函数MySQL DENSE_RANK() 函数MySQL FIRST_VALUE 函数MySQL LAG() 函数MySQL LAST_VALUE() 函数MySQL LEAD 函数MySQL NTH_VALUE 函数MySQL NTILE 函数MySQL PERCENT_RANK 函数MySQL RANK 函数MySQL ROW_NUMBER 函数

MySQL 应用

MySQL应用程序编程接口PHP MySQL教程PHP 数据库连接Python 持久化实现

MySQL 优化

mysqld 内存持续变高求字符串交集


MySQL LAST_VALUE() 函数


简介:在本教程中,您将学习如何使用MySQL LAST_VALUE()函数返回有序行集中的最后一行。

MySQL LAST_VALUE() 函数概述

LAST_VALUE()函数是一个窗口函数,允许您选择有序行集中的最后一行。 以下显示了LAST_VALUE()函数的语法:
LAST_VALUE (expression) OVER (
    [partition_clause]
    [order_clause]
    [frame_clause]
)
LAST_VALUE()函数返回expression有序行集的最后一行的值。 OVER有三个子句:partition_clauseorder_clause,和frame_clause

partition_clause

partition_clause语法如下:
PARTITION BY expr1, expr2, ...
PARTITION BY子句分配结果集成由一个或多个表达式指定多个分区expr1expr2LAST_VALUE()函数被独立地施加到每个分区。

order_clause

order_clause语法如下:
ORDER BY  expr1 [ASC|DESC],...
ORDER BY子句指定LAST_VALUE()函数运行的分区中行的逻辑顺序。

 frame_clause

frame_clause定义了所述当前分区的所述子集LAST_VALUE()函数应用。有关更多详细信息frame_clause,请查看窗口功能教程

MySQL LAST_VALUE() 函数示例

让我们设置一个示例表进行演示。 以下是创建overtime表并将数据填充到表中的脚本。
CREATE TABLE overtime (
    employee_name VARCHAR(50) NOT NULL,
    department VARCHAR(50) NOT NULL,
    hours INT NOT NULL,
     PRIMARY KEY (employee_name , department)
);

INSERT INTO overtime(employee_name, department, hours)
VALUES('Diane Murphy','Accounting',37),
('Mary Patterson','Accounting',74),
('Jeff Firrelli','Accounting',40),
('William Patterson','Finance',58),
('Gerard Bondur','Finance',47),
('Anthony Bow','Finance',66),
('Leslie Jennings','IT',90),
('Leslie Thompson','IT',88),
('Julie Firrelli','Sales',81),
('Steve Patterson','Sales',29),
('Foon Yue Tseng','Sales',65),
('George Vanauf','Marketing',89),
('Loui Bondur','Marketing',49),
('Gerard Hernandez','Marketing',66),
('Pamela Castillo','SCM',96),
('Larry Bott','SCM',100),
('Barry Jones','SCM',65);
mysql> select * from overtime;
+-------------------+------------+-------+
| employee_name     | department | hours |
+-------------------+------------+-------+
| Anthony Bow       | Finance    |    66 |
| Barry Jones       | SCM        |    65 |
| Diane Murphy      | Accounting |    37 |
| Foon Yue Tseng    | Sales      |    65 |
| George Vanauf     | Marketing  |    89 |
| Gerard Bondur     | Finance    |    47 |
| Gerard Hernandez  | Marketing  |    66 |
| Jeff Firrelli     | Accounting |    40 |
| Julie Firrelli    | Sales      |    81 |
| Larry Bott        | SCM        |   100 |
| Leslie Jennings   | IT         |    90 |
| Leslie Thompson   | IT         |    88 |
| Loui Bondur       | Marketing  |    49 |
| Mary Patterson    | Accounting |    74 |
| Pamela Castillo   | SCM        |    96 |
| Steve Patterson   | Sales      |    29 |
| William Patterson | Finance    |    58 |
+-------------------+------------+-------+
17 rows in set (0.01 sec)

1)MySQL LAST_VALUE()在整个查询结果示例中

以下语句获取员工姓名,加班时间和加班时间最长的员工:
SELECT
    employee_name,
    hours,
    LAST_VALUE(employee_name) OVER (
        ORDER BY hours
        RANGE BETWEEN
            UNBOUNDED PRECEDING AND
            UNBOUNDED FOLLOWING
    ) highest_overtime_employee
FROM
    overtime;
输出是:
+-------------------+-------+---------------------------+
| employee_name     | hours | highest_overtime_employee |
+-------------------+-------+---------------------------+
| Steve Patterson   |    29 | Larry Bott                |
| Diane Murphy      |    37 | Larry Bott                |
| Jeff Firrelli     |    40 | Larry Bott                |
| Gerard Bondur     |    47 | Larry Bott                |
| Loui Bondur       |    49 | Larry Bott                |
| William Patterson |    58 | Larry Bott                |
| Barry Jones       |    65 | Larry Bott                |
| Foon Yue Tseng    |    65 | Larry Bott                |
| Anthony Bow       |    66 | Larry Bott                |
| Gerard Hernandez  |    66 | Larry Bott                |
| Mary Patterson    |    74 | Larry Bott                |
| Julie Firrelli    |    81 | Larry Bott                |
| Leslie Thompson   |    88 | Larry Bott                |
| George Vanauf     |    89 | Larry Bott                |
| Leslie Jennings   |    90 | Larry Bott                |
| Pamela Castillo   |    96 | Larry Bott                |
| Larry Bott        |   100 | Larry Bott                |
+-------------------+-------+------------------------
在此示例中,ORDER BY子句将结果集中行的逻辑顺序指定为从低到高的小时。 默认帧规范如下:
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
这意味着框架从第一行开始,到结果集的当前行结束。 因此,为了获得加班时间最长的员工,我们将框架规格更改为以下内容:
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
这表示框架从第一行开始,到结果集的最后一行结束。

2)MySQL LAST_VALUE()上的分区示例

以下语句查找每个部门加班时间最长的员工:
SELECT
    employee_name,
    department,
    hours,
    LAST_VALUE(employee_name) OVER (
 PARTITION BY department
        ORDER BY hours
        RANGE BETWEEN
     UNBOUNDED PRECEDING AND
            UNBOUNDED FOLLOWING
 ) most_overtime_employee
FROM
    overtime;
下图显示了输出:
+-------------------+------------+-------+------------------------+
| employee_name     | department | hours | most_overtime_employee |
+-------------------+------------+-------+------------------------+
| Diane Murphy      | Accounting |    37 | Mary Patterson         |
| Jeff Firrelli     | Accounting |    40 | Mary Patterson         |
| Mary Patterson    | Accounting |    74 | Mary Patterson         |
| Gerard Bondur     | Finance    |    47 | Anthony Bow            |
| William Patterson | Finance    |    58 | Anthony Bow            |
| Anthony Bow       | Finance    |    66 | Anthony Bow            |
| Leslie Thompson   | IT         |    88 | Leslie Jennings        |
| Leslie Jennings   | IT         |    90 | Leslie Jennings        |
| Loui Bondur       | Marketing  |    49 | George Vanauf          |
| Gerard Hernandez  | Marketing  |    66 | George Vanauf          |
| George Vanauf     | Marketing  |    89 | George Vanauf          |
| Steve Patterson   | Sales      |    29 | Julie Firrelli         |
| Foon Yue Tseng    | Sales      |    65 | Julie Firrelli         |
| Julie Firrelli    | Sales      |    81 | Julie Firrelli         |
| Barry Jones       | SCM        |    65 | Larry Bott             |
| Pamela Castillo   | SCM        |    96 | Larry Bott             |
| Larry Bott        | SCM        |   100 | Larry Bott             |
+-------------------+------------+-------+------------------------+
17 rows in set (0.02 sec)
在这个例子中,首先,PARTITION BY子句按部门划分了员工。然后,ORDER BY子句通过加班从低到高命令每个部门的员工。 在这种情况下,帧规范是整个分区。结果,LAST_VALUE()函数选择了每个分区中的最后一行,分区是加班时间最高的员工。 在本教程中,您学习了如何使用MySQL LAST_VALUE()函数获取有序行集中的最后一行。