SELECT bd.SENSOR_ID as sensorId, si.sensor_name AS sensorName, si.dam_id as damId, bd.WATER_LEVEL AS waterLevel, bd.WATER_TOP AS waterTop , bd.HOLE_DEPTH AS holeDepth, td.height-bd.WATER_TOP AS buryDepth FROM ( SELECT DAM_ID,SENSOR_ID, WATER_LEVEL,WATER_TOP, HOLE_DEPTH FROM B_DAM_WATER_LEVEL bd1 WHERE bd1.RN = 1 AND EXISTS ( SELECT 1 FROM B_DAM_WATER_LEVEL bd2 WHERE bd2.SENSOR_ID = bd1.SENSOR_ID AND bd2.UPLOAD_TIME > bd1.UPLOAD_TIME ) ) bd WHERE bd.SENSOR_ID = si.sensor_id AND bd.DAM_ID = td.dam_id; 这个语句是错误的
时间: 2024-04-28 20:22:19 浏览: 228
这个 SQL 查询语句确实有一些语法错误,具体如下:
1. 在 SELECT 子句中,引用了别名为 "si" 的表,但没有在 FROM 子句中声明这个表。
2. 在 WHERE 子句中,引用了别名为 "td" 的表,但没有在 FROM 子句中声明这个表。
3. 在 SELECT 子句中,引用了别名为 "sensorId" 的列,但没有在任何一个表中声明这个列。
下面是修正后的 SQL 查询语句:
SELECT bd.SENSOR_ID AS sensorId,
si.sensor_name AS sensorName,
si.dam_id AS damId,
bd.WATER_LEVEL AS waterLevel,
bd.WATER_TOP AS waterTop,
bd.HOLE_DEPTH AS holeDepth,
td.height - bd.WATER_TOP AS buryDepth
FROM (
SELECT DAM_ID, SENSOR_ID, WATER_LEVEL, WATER_TOP, HOLE_DEPTH
FROM B_DAM_WATER_LEVEL bd1
WHERE bd1.RN = 1
AND EXISTS (
SELECT 1
FROM B_DAM_WATER_LEVEL bd2
WHERE bd2.SENSOR_ID = bd1.SENSOR_ID
AND bd2.UPLOAD_TIME > bd1.UPLOAD_TIME
)
) bd
JOIN SENSOR_INFO si ON bd.SENSOR_ID = si.sensor_id
JOIN DAM_INFO td ON bd.DAM_ID = td.dam_id;
阅读全文