【强强联合】使用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 查询历史监控指标信息


四、查询效果

在这里插入图片描述

相关内容

热门资讯

罗永浩录音还未公布,华与华兄弟... 澎湃新闻记者 戴高城12月22日,读客文化(301025.SZ)的一纸停牌公告,将华楠、华杉兄弟推上...
万亿长沙银行换帅,“70后”女... 出品|达摩财经12月19日,长沙银行(601577.SH)发布公告称,该行董事会收到董事长赵小中的辞...
54岁恒隆集团CEO卢韦柏官宣... 红星资本局12月22日消息,日前,恒隆集团(00010.HK)、恒隆地产(00101.HK)联合公告...
智谱、MiniMax冲刺港交所... 出品|达摩财经AI独角兽正加速冲向资本市场,争夺“大模型第一股”的位置。12月21日,通用人工智能(...
吉利汽车与极氪整合完成 新京报贝壳财经讯 12月22日,吉利汽车控股有限公司(0175.HK)(简称“吉利汽车”)发布公告,...