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


四、查询效果

在这里插入图片描述

相关内容

热门资讯

分享技巧“杭州都莱到底是有挂吗... 您好:杭州都莱这款游戏可以开挂,确实是有挂的,需要了解加客服微信【3636476】很多玩家在这款游戏...
盘点一款“中至打炸有没有挂”其... 亲.中至打炸这款游戏是可以开挂的,确实是有挂的,通过添加客服【3671900】很多玩家在这款游戏中怀...
重大通报“新猴王大厅能不能开透... 您好:新猴王大厅这款游戏可以开挂,确实是有挂的,需要软件加微信【6355786】,很多玩家在新猴王大...
玩家实测“吉祥棋牌能不能开挂”... 您好:吉祥棋牌这款游戏可以开挂,确实是有挂的,需要软件加微信【5951795】,很多玩家在吉祥棋牌这...
实测分享“心悦海南麻将有没有挂... 您好:心悦海南麻将这款游戏可以开挂,确实是有挂的,需要软件加微信【69174242】,很多玩家在心悦...