简体   繁体   English

具有自联接访问权限的子查询,无法识别表别名

[英]Subquery in access with self-join, it does not recognize table alias

I built some subquery: 我建立了一些子查询:

select 
    q2.addedquests, q2.daynum
from 
    qrNumberOfQuestsToDo as q2 
inner join 
    qrNumberOfQuestsToDo as q3 on q2.daynum > q3.daynum
where 
    q2.DayNum = (select max(q3.DayNum) from q3);

but MS Access does not recognize q3 in the subquery. 但是MS Access无法在子查询中识别q3 Why? 为什么?

Why not? 为什么不? Because that is how SQL works. 因为这就是SQL的工作方式。 You can refer to a column of q3 in the subquery (in the select , where , group by , having , order by , or on clause for instance), but not to the entire table. 您可以在子查询中引用 q3的列(例如,在selectwheregroup byhavingorder byon子句中),但不能引用整个表。

I am continuing, but I think the query is non-sensical. 我正在继续,但我认为查询是无意义的。 In one place, the query says that q2.daynum > q3.daynum . 在一个地方,查询说q2.daynum > q3.daynum In another, that q2.daynum = q3.daynum . 在另一个q2.daynum = q3.daynumq2.daynum = q3.daynum Hence, the query will not return anything. 因此,查询将不会返回任何内容。 But, you can still express it as valid SQL. 但是,您仍然可以将其表示为有效的SQL。 Notice that q3 is not really needed in the outer query. 请注意,外部查询中实际上并不需要q3 Try a correlated subquery instead: 请尝试使用相关子查询:

select q2.addedquests, q2.daynum
from qrNumberOfQuestsToDo as q2 
where q2.DayNum = (select max(q3.DayNum)
                   from qrNumberOfQuestsToDo q3
                   where q2.daynum>q3.daynum
                  )  ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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