简体   繁体   中英

Sql in postgres: select every row in one table that is related to other and other

Hi i'm new at postgresql .

i have this database:

数据库

Actually, I have a table "borda" that relates to the table "gateway" this table "gateway" relates to the table "equipamento" and this table relates to the table "sensor".

The question is: I have the "borda_id" from Borda table. Now i want every sensor_id relationed to this "borda_id".

How should I write the sql?

Thanks..

The Publicacao Table is the easiest way to associate the Sensor table with the Borda table. The image describes that Publicacao should exist with both a sensor and a borda associated.

You can capture all the rows that exists in both tables (Publicacao and Sensor) with INNER JOIN:

 SELECT Sensor.nome
    ,Sensor.descricao
    ,Sensor.modelo
    ,Sensor.precisao
    ,Sensor.valorMin
    ,Sensor.valorMax
    ,Sensor.fabricante_id
    ,Sensor.tipoSensor_id
    ,Sensor.equipamento_id
FROM Sensor
INNER JOIN Publicacao ON Sensor_id = Publicacao.sensor_id

Now you have all the Sensors that is associated with a Publicacao.

In order to achieve what you want, we need to filter the result to get "All the Sensors that is associated with a Publicacao that has a borda_id equals X"

SELECT Sensor.nome
    ,Sensor.descricao
    ,Sensor.modelo
    ,Sensor.precisao
    ,Sensor.valorMin
    ,Sensor.valorMax
    ,Sensor.fabricante_id
    ,Sensor.tipoSensor_id
    ,Sensor.equipamento_id
FROM Sensor
INNER JOIN Publicacao ON Sensor_id = Publicacao.sensor_id
WHERE Publicacao.borda_id = X

X being the borda_id that you want to reference in Borda table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM