[英]join two tables data in third table
I have two tables ,one sensor_info and other one is bays_info.here are the tables 我有两个表,一个是sensor_info,另一个是bays_info。这是这些表
id Sensor_Name stn_id value Station_id
1 Sensor1 0 4.4 1
2 Sensor2 0 2.1 1
3 Sensor3 0 3.2 1
4 Sensor4 1 4.5 1
Bays_info Bays_info
gate_id bay_name bay_status Station_id
GATE1 B_gate1 1 1
GATE2 B_gate2 -999 1
GATE3 B_gate2 1 1
GATE4 B_gate2 -999 1
GATE5 B_gate2 1 1
Friends I need to show all "sensors value" for each gate, like below (here station id is common col) 朋友,我需要显示每个闸门的所有“传感器值”,如下所示(此处的站号是公共栏)
gate_id bay_name bay_status Station_id Sensor1 Sensor2 sensor3 sensor4
GATE1 B_gate1 1 1 4.4 2.1 3.2 4.5
GATE2 B_gate2 -999 1 4.4 2.1 3.2 4.5
GATE3 B_gate2 1 1 4.4 2.1 3.2 4.5
GATE4 B_gate2 -999 1 4.4 2.1 3.2 4.5
GATE5 B_gate2 1 1 4.4 2.1 3.2 4.5
So I need to show value from sensor table for each station and also need to show sensor names as header.Please help me for MySQL query. 因此,我需要显示每个站的传感器表中的值,还需要显示传感器名称作为标题。请帮助我进行MySQL查询。
try this 尝试这个
select gate_id , bay_name , bay_status , bays_info.Station_id,
max(case when Sensor_Name = 'US.SEN1' then round(value,2) end) as Sensor1,
max(case when Sensor_Name = 'US.SEN2' then round(value,2) end) as Sensor2,
max(case when Sensor_Name = 'US.SEN3' then round(value,2) end) as Sensor3,
max(case when Sensor_Name = 'DS.SEN1' then round(value,2) end) as Sensor4,
max(case when Sensor_Name = 'DS.SEN2' then round(value,2) end) as Sensor5,
max(case when Sensor_Name = 'DS.SEN3' then round(value,2) end) as Sensor6,
max(case when Sensor_Name = 'MS.SEN1' then round(value,2) end) as Sensor7
from bays_info
inner join sensor_info
on sensor_info.Station_id = bays_info.Station_id
group by gate_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.