iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >【mysql 视图】10分钟教你每天自动更新视图
  • 115
分享到

【mysql 视图】10分钟教你每天自动更新视图

mysql数据库github 2023-09-04 13:09:39 115人浏览 独家记忆
摘要

Mysql 视图是一个强大的工具,可以简化复杂的查询操作,并且保护敏感数据。在 mysql 中,可以通过 CREATE VIEW 语句创建视图,在查询中使用视图,并且可以使用 DROP VIE

Mysql 视图是一个强大的工具,可以简化复杂的查询操作,并且保护敏感数据。在 mysql 中,可以通过 CREATE VIEW 语句创建视图,在查询中使用视图,并且可以使用 DROP VIEW 语句删除视图。需要注意的是,Mysql 视图通常是只读的。
假设我有如下语句,需要给下面语句创建视图,并自动每天更新,这样每次查询视图看到的就是最新的结果了。

select  substr(create_time,1,10) as 'cjsj',ifnull(round(STDDEV_SAMP(case tail_coal_dcs_switch when '1'  then calciner_temperature  end),3),0) as 'calciner_temperature_1',ifnull(round(STDDEV_SAMP(case tail_coal_dcs_switch when '0' then calciner_temperature  end),3),0) as 'calciner_temperature_0',ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '1'  then two_temperature end),3),0) as 'two_temperature_1',ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '0' then two_temperature end),3),0) as 'two_temperature_0',ifnull(round(STDDEV_SAMP(case tou_pai_dcs_switch when '1'  then yao_tou_pressure end),3),0) as 'yao_tou_pressure_1',ifnull(round(STDDEV_SAMP(case tou_pai_dcs_switch when '0' then yao_tou_pressure end),3),0) as 'yao_tou_pressure_0',ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '1'  then f2_p  end),3),0) as 'f2_p_1',ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '0' then f2_p end),3),0) as 'f2_p_0',ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '1'  then shu_liao_temperature  end),3),0) as 'shu_liao_temperature_1',ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '0' then shu_liao_temperature end),3),0) as 'shu_liao_temperature_0',ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '1'  then aqc_temperature  end),3),0) as 'aqc_temperature_1',ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '0' then aqc_temperature  end),3),0) as 'aqc_temperature_0',ifnull(round(STDDEV_SAMP(case head_coal_dcs_switch when '1'  then fcao    end),3),0) as 'fcao_1',ifnull(round(STDDEV_SAMP(case head_coal_dcs_switch when '0' then fcao end),3),0) as 'fcao_0'from kiln_system_dcs_switchgroup  by substr(create_time,1,10)

这个 SQL 查询语句的作用是查询 kiln_system_dcs_switch 表中指定时间段内(可通过 WHERE 子句控制)的各项数据的标准差,结果按照日期分组展示。如果需要自动更新查询结果,可以使用事件调度器(Event Scheduler)实现,定期执行该查询语句并将结果存储到指定表中。

可以使用以下 SQL 语句将上述查询结果创建为视图:

CREATE VIEW kiln_system_data_stddev ASSELECT      substr(create_time,1,10) AS 'cjsj',    ifnull(round(STDDEV_SAMP(case tail_coal_dcs_switch when '1'  then calciner_temperature  end),3),0) as 'calciner_temperature_1',    ifnull(round(STDDEV_SAMP(case tail_coal_dcs_switch when '0' then calciner_temperature  end),3),0) as 'calciner_temperature_0',    ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '1'  then two_temperature end),3),0) as 'two_temperature_1',    ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '0' then two_temperature end),3),0) as 'two_temperature_0',    ifnull(round(STDDEV_SAMP(case tou_pai_dcs_switch when '1'  then yao_tou_pressure end),3),0) as 'yao_tou_pressure_1',    ifnull(round(STDDEV_SAMP(case tou_pai_dcs_switch when '0' then yao_tou_pressure end),3),0) as 'yao_tou_pressure_0',    ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '1'  then f2_p  end),3),0) as 'f2_p_1',    ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '0' then f2_p end),3),0) as 'f2_p_0',    ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '1'  then shu_liao_temperature  end),3),0) as 'shu_liao_temperature_1',    ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '0' then shu_liao_temperature end),3),0) as 'shu_liao_temperature_0',    ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '1'  then aqc_temperature  end),3),0) as 'aqc_temperature_1',    ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '0' then aqc_temperature  end),3),0) as 'aqc_temperature_0',    ifnull(round(STDDEV_SAMP(case head_coal_dcs_switch when '1'  then fcao    end),3),0) as 'fcao_1',    ifnull(round(STDDEV_SAMP(case head_coal_dcs_switch when '0' then fcao end),3),0) as 'fcao_0'FROM kiln_system_dcs_switchGROUP BY substr(create_time,1,10);

这个 SQL 语句将查询结果以视图的形式保存到数据库中,方便后续查询和分析。视图名为 kiln_system_data_stddev,包含了和之前查询语句中相同的列和数据。创建视图后,可以直接通过以下 SQL 语句来查询视图内容:

SELECT * FROM kiln_system_data_stddev;

创建视图后,视图中的查询结果并不会自动更新,需要手动执行 SQL 查询语句或者使用事件调度器(Event Scheduler)来定期更新视图内容。

如果你需要每天自动更新视图内容,可以考虑使用事件调度器。例如,以下是一个每天凌晨 3:00 自动更新视图的例子:

CREATE EVENT update_kiln_system_data_stddevON SCHEDULE EVERY 1 DAY STARTS '2023-04-26 03:00:00'DO    REPLACE INTO kiln_system_data_stddev    SELECT          substr(create_time,1,10) AS 'cjsj',        ifnull(round(STDDEV_SAMP(case tail_coal_dcs_switch when '1' then calciner_temperature end), 3), 0) AS 'calciner_temperature_1',        ifnull(round(STDDEV_SAMP(case tail_coal_dcs_switch when '0' then calciner_temperature end), 3), 0) AS 'calciner_temperature_0',        ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '1' then two_temperature end), 3), 0) AS 'two_temperature_1',        ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '0' then two_temperature end), 3), 0) AS 'two_temperature_0',        ifnull(round(STDDEV_SAMP(case tou_pai_dcs_switch when '1' then yao_tou_pressure end), 3), 0) AS 'yao_tou_pressure_1',        ifnull(round(STDDEV_SAMP(case tou_pai_dcs_switch when '0' then yao_tou_pressure end), 3), 0) AS 'yao_tou_pressure_0',        ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '1' then f2_p end), 3), 0) AS 'f2_p_1',        ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '0' then f2_p end), 3), 0) AS 'f2_p_0',        ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '1' then shu_liao_temperature end), 3), 0) AS 'shu_liao_temperature_1',        ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '0' then shu_liao_temperature end), 3), 0) AS 'shu_liao_temperature_0',        ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '1' then aqc_temperature end), 3), 0) AS 'aqc_temperature_1',        ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '0' then aqc_temperature end), 3), 0) AS 'aqc_temperature_0',        ifnull(round(STDDEV_SAMP(case head_coal_dcs_switch when '1' then fcao end), 3), 0) AS 'fcao_1',        ifnull(round(STDDEV_SAMP(case head_coal_dcs_switch when '0' then fcao end), 3), 0) AS 'fcao_0'    FROM kiln_system_dcs_switch    GROUP BY substr(create_time, 1, 10);

这个 SQL 语句创建了一个名为 update_kiln_system_data_stddev 的事件调度器,每天凌晨 3:00 执行一次查询语句,将结果更新到视图 kiln_system_data_stddev 中。需要注意的是,这个事件调度器的开始时间为 ‘2023-04-26 03:00:00’,需要根据实际情况进行修改。

创建好事件调度器后,可以执行以下 SQL 语句查看当前所有的事件调度器:

SHOW EVENTS;

如果需要停止某个事件调度器,可以使用以下 SQL 语句:

DROP EVENT update_kiln_system_data_stddev;

要让上面的事件调度器工作起来,首先得查看事件有没有开启,必须开启状态才能工作。
可以使用以下 SQL 语句查看 MySQL 中是否启用了事件调度器:

SELECT @@event_scheduler;

如果返回值为 ON,则表示事件调度器已经启用;如果返回值为 OFF,则表示事件调度器尚未启用。
如果返回值为 OFF,可以通过执行以下 SQL 语句来开启事件调度器:

SET GLOBAL event_scheduler = ON;

此时再执行 SELECT @@event_scheduler; 应该就能返回 ON 了。

来源地址:https://blog.csdn.net/u013421629/article/details/130379884

您可能感兴趣的文档:

--结束END--

本文标题: 【mysql 视图】10分钟教你每天自动更新视图

本文链接: https://www.lsjlt.com/news/393611.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • oracle怎么查询当前用户所有的表
    要查询当前用户拥有的所有表,可以使用以下 sql 命令:select * from user_tables; 如何查询当前用户拥有的所有表 要查询当前用户拥有的所有表,可以使...
    99+
    2024-05-14
    oracle
  • oracle怎么备份表中数据
    oracle 表数据备份的方法包括:导出数据 (exp):将表数据导出到外部文件。导入数据 (imp):将导出文件中的数据导入表中。用户管理的备份 (umr):允许用户控制备份和恢复过程...
    99+
    2024-05-14
    oracle
  • oracle怎么做到数据实时备份
    oracle 实时备份通过持续保持数据库和事务日志的副本来实现数据保护,提供快速恢复。实现机制主要包括归档重做日志和 asm 卷管理系统。它最小化数据丢失、加快恢复时间、消除手动备份任务...
    99+
    2024-05-14
    oracle 数据丢失
  • oracle怎么查询所有的表空间
    要查询 oracle 中的所有表空间,可以使用 sql 语句 "select tablespace_name from dba_tablespaces",其中 dba_tabl...
    99+
    2024-05-14
    oracle
  • oracle怎么创建新用户并赋予权限设置
    答案:要创建 oracle 新用户,请执行以下步骤:以具有 create user 权限的用户身份登录;在 sql*plus 窗口中输入 create user identified ...
    99+
    2024-05-14
    oracle
  • oracle怎么建立新用户
    在 oracle 数据库中创建用户的方法:使用 sql*plus 连接数据库;使用 create user 语法创建新用户;根据用户需要授予权限;注销并重新登录以使更改生效。 如何在 ...
    99+
    2024-05-14
    oracle
  • oracle怎么创建新用户并赋予权限密码
    本教程详细介绍了如何使用 oracle 创建一个新用户并授予其权限:创建新用户并设置密码。授予对特定表的读写权限。授予创建序列的权限。根据需要授予其他权限。 如何使用 Oracle 创...
    99+
    2024-05-14
    oracle
  • oracle怎么查询时间段内的数据记录表
    在 oracle 数据库中查询指定时间段内的数据记录表,可以使用 between 操作符,用于比较日期或时间的范围。语法:select * from table_name wh...
    99+
    2024-05-14
    oracle
  • oracle怎么查看表的分区
    问题:如何查看 oracle 表的分区?步骤:查询数据字典视图 all_tab_partitions,指定表名。结果显示分区名称、上边界值和下边界值。 如何查看 Oracle 表的分区...
    99+
    2024-05-14
    oracle
  • oracle怎么导入dump文件
    要导入 dump 文件,请先停止 oracle 服务,然后使用 impdp 命令。步骤包括:停止 oracle 数据库服务。导航到 oracle 数据泵工具目录。使用 impdp 命令导...
    99+
    2024-05-14
    oracle
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作