简体   繁体   English

在第三张表中联接两个表数据

[英]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

DEMO HERE 此处演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 连接两个表并在MySql中更新第三个表 - Join two tables and update third table in MySql 如何连接两个表并按第三个表排序? - How to join two tables and order by a third table? MYSQL 基于第三个表中的关系连接两个表并连接第二个表中的数据 - MYSQL JOIN two tables based on a relation in third table and concat the data from the second table mysql:将具有两个字段的两个表联接在一起以获得第三个表 - mysql: join two tables with two fields in common to get a third table 如何联接两个表,同时根据引用第三个表的条件排除数据? - How do you join two tables, while excluding data based on a condition referencing a third table? mysql-当“ where”子句来自第三个表时,在两个表之间连接数据 - mysql - join data between two tables when “where” clause comes from a third table 来自两个表的SQL计数,具有来自第三个表的内部联接 - SQL Count from Two Tables with Inner Join from Third Table 连接两个表,但排除匹配第三个表中条件的行 - Join two tables but exclude rows that match condition in a third table MySql从两个表中多次选择并将它们的结果连接到第三个表 - MySql multiple select from two tables and join their results to a third table MySql对两个表中的记录求和,并与第三个表联接 - MySql Sum the records in two tables and join with the third table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM