SQL 语法错误 "WITH" 子句
SQL syntax error with "WITH" clause
在 WHERE 子句中使用 WITH 别名时遇到一个奇怪的问题。
问题 SQL 语句:
WITH CROOM AS (SELECT id FROM classroom WHERE floor = 1)
SELECT DISTINCT teacher_id
FROM timetable
WHERE classroom_id IN CROOM;
错误是ORA-00904: "CROOM": invalid identifier
虽然这两个语义相似的语句都很有效。
SELECT DISTINCT teacher_id
FROM timetable
WHERE classroom_id IN (SELECT id FROM classroom WHERE floor = 1);
WITH CROOM AS (SELECT id FROM classroom WHERE floor = 1)
SELECT DISTINCT T.teacher_id
FROM timetable T JOIN CROOM ON (T.classroom_id = CROOM.id);
为什么在 FROM 子句中接受别名却无法识别 WHERE 中的别名?
您应该将 CROOM 用作 table。你可以这样写:
WITH CROOM AS (SELECT id FROM classroom WHERE floor = 1)
SELECT DISTINCT teacher_id
FROM timetable
WHERE classroom_id IN (select id from CROOM);
在 WHERE 子句中使用 WITH 别名时遇到一个奇怪的问题。 问题 SQL 语句:
WITH CROOM AS (SELECT id FROM classroom WHERE floor = 1)
SELECT DISTINCT teacher_id
FROM timetable
WHERE classroom_id IN CROOM;
错误是ORA-00904: "CROOM": invalid identifier
虽然这两个语义相似的语句都很有效。
SELECT DISTINCT teacher_id
FROM timetable
WHERE classroom_id IN (SELECT id FROM classroom WHERE floor = 1);
WITH CROOM AS (SELECT id FROM classroom WHERE floor = 1)
SELECT DISTINCT T.teacher_id
FROM timetable T JOIN CROOM ON (T.classroom_id = CROOM.id);
为什么在 FROM 子句中接受别名却无法识别 WHERE 中的别名?
您应该将 CROOM 用作 table。你可以这样写:
WITH CROOM AS (SELECT id FROM classroom WHERE floor = 1)
SELECT DISTINCT teacher_id
FROM timetable
WHERE classroom_id IN (select id from CROOM);