当我运行此查询时,Oracle给了我一个错误(ORA-00907:缺少右括号):
select *
from reason_for_appointment
where reason_for_appointment_id in
(
select reason_for_appointment_id
from appointment_reason
where appointment_id = 11
order by appointment_reason_id
)
但是,当我只运行子查询时,没有错误。
任何人都可以解释问题是什么吗?
答案 0 :(得分:11)
内部查询结果将永远不会显示,因此在嵌套选择中执行顺序没有意义。而是将其应用于外部查询。
答案 1 :(得分:3)
问题是在这样的子查询中不允许使用ORDER BY。你为什么想要一个?
答案 2 :(得分:1)
看起来您希望使用另一个表中定义的排序显示一个表的结果。内连接应该足够了。
select reason_for_appointment.*
from reason_for_appointment rfa, appointment_reason ar
where rfa.reason_for_appointment_id = ar.reason_for_appointment_id
and ar.appointment_id = 11
order by ar.appointment_reason_id;
答案 3 :(得分:0)
从reason_for_appointment中选择*,其中reason_for_appointment_id in(从appointment_reason中选择reason_for_appointment_id,其中appointment_id = 11 by appointment_reason_id)
尝试以下方法: 使用辅助作为(从appointment_reason中选择reason_for_appointment_id,其中appointment_id =按order_reason_id排序11) 从appointment_reason中选择reason_for_appointment_id,其中reason_for_appointment_id(从辅助中选择reason_for_appointment_id)
答案 4 :(得分:0)
如果你的目标是订购输出,你只需要在子查询之外移动ORDER BY:
select * from reason_for_appointment where reason_for_appointment_id in
(select reason_for_appointment_id from appointment_reason where appointment_id = 11)
order by reason_for_appointment_id
(我假设你在哪里写了“appointment_reason_id”,你的意思是“reason_for_appointment_id”。如果确实有两个不同的列有这些名字......哎哟。)