【强强联合】使用MySQL UNION ALL功能解决同时显示两张表的数据问题,显示物联网设备地理位置和其它监控指标
创始人
2025-05-29 00:21:09

一、背景

应业主方需求,设备监控指标不要显示经纬度值,而是显示其对应的地理位置信息。但是设计的时候,多维度的设备监控指标是在一张表中存储,另一张表存储设备的经纬度值及其对应的地理位置。

接到需求时,想着直接更改库表来完成需求,但是改动的地方太多,改完了还要进行测试,因此就多方寻找解决方案,后来灵机一动,想着MySQL应该有拼接的功能,就找到了UNION ALL指令。

二、表结构

2.1 设备监控数据表

create table device_kv
(uuid                varchar(32)     not null comment '序号'primary key,device_id           bigint unsigned not null comment '设备ID',metric_union_key_id int             not null comment '指标联合编码',metric_value        varchar(255)    null comment '指标值',user_id             bigint          null comment '用户ID',latest_time         datetime        not null comment '数据采集时间'
)comment '设备监控数据';create index idx_device_idon device_kv (device_id);create index idx_latest_timeon device_kv (latest_time);

该表用于存储每个设备的多维度遥测指标值,其中metric_union_key_id等于8时对应着经纬度

2.2 设备监控指标最新数据表

create table device_kv_latest
(uuid                varchar(32)               not null comment '序号'primary key,device_id           bigint unsigned           not null comment '设备号',metric_union_key_id int                       not null comment '指标联合编码ID',metric_value        varchar(255)              null comment '指标值',create_time         datetime                  null comment '关联时间',latest_time         datetime                  not null comment '最新时间',user_id             bigint unsigned default 0 not null comment '用户ID'
)comment '设备监控指标最新数据';create index idx_device_idon device_kv_latest (device_id);create index idx_latest_timeon device_kv_latest (latest_time);

该表用于存储设备最新的遥测数据信息。

2.3 设备定位数据表

create table device_location_track
(id        varchar(32)     not null comment '设备位置ID'primary key,device_id bigint unsigned not null comment '设备ID',gps_data  varchar(100)    null comment 'GPS数据',lng       double          null,lat       double          null,address   varchar(255)    null comment '地理解析位置',data_time datetime        null comment '数据时间',user_id   bigint unsigned null comment '用户ID'
)comment '设备定位数据';create index idx_data_timeon device_location_track (data_time)comment '数据采集时间索引';create index idx_device_idon device_location_track (device_id)comment '设备ID索引';

该表用于存储设备定位数据信息。

三、查询

项目基于Mybatis-plus半自动 ORM 框架进行开发。

3.1 查询最新的监控指标信息

 

3.2 查询历史监控指标信息


四、查询效果

在这里插入图片描述

相关内容

热门资讯

(轻松.掌握)中至吉安麻将要开... (轻松.掌握)中至吉安麻将要开挂用什么软件[分享装挂详细步骤]【无需打开直接搜索微信【4579337...
重大通报“逗娱碰胡是不是有透视... 您好:逗娱碰胡这款游戏可以开挂,确实是有挂的,需要了解加客服微信【4194432】很多玩家在逗娱碰胡...
今日重大通报“宜春情怀麻将到底... 无需打开直接搜索微信【2832208】本司针对手游进行,选择我们的四大理由:宜春情怀麻将这款游戏其实...
玩家揭秘先锋大厅开挂神器开挂详... 您好:先锋大厅这款游戏可以开挂,确实是有挂的,需要了解加客服微信【3354922】很多玩家在这款游戏...
科普实测“新永和大厅透视挂辅助... 您好:新永和大厅这款游戏可以开挂,确实是有挂的,需要软件加微信【6355786】,很多玩家在新永和大...