我的表格结构如下:
measurements:
id | Value | sensor_id
现在我有另一个表叫做传感器,
sensors:
id | Name | provider_id
然后我有另一个名为Providers的表:
providers:
id | Name
现在我要做的是从测量表中选择sensor_ID具有特定类型(由ID指定)的所有记录。
所以我做了类似的事情:
select * from measurements, sensors, providers
JOIN sensors on sensors.id = measurements.sensor_id
JOIN providers on providers.id = sensor.provider_id
where provider.id = 1 LIMIT 100
但是,There is an entry for table "sensor_measurements", but it cannot be referenced from this part of the query.
回来了,我已经尝试了其他类型的变体,但是无法使其正常工作。
我正在使用Postgresql和PgAdmin工具。
答案 0 :(得分:1)
从不在FROM
子句中使用逗号。所以:
select * -- you should list out the columns you want
from measurements m join
sensors s
on s.id = m.sensor_id join
providers p
on p.id = s.provider_id
where p.id = 1
limit 100
答案 1 :(得分:1)
在我看来,您的FROM子句是错误的。 应该是:
FROM
measurements
JOIN sensors on sensors.id = measurements.sensor_id
JOIN providers on providers.id = sensor.provider_id
或者
FROM
measurements,
sensors,
providers
WHERE
providers.id = 1 and
sensors.provider_id = providers.id
measurements.sensor_id = sensors.id
您当前正在包含所需的3个表,但除此之外,您又又加入了sensors和provider表,总共有5个(据我所知)。