简体   繁体   中英

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. Why?

Why not? Because that is how SQL works. 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.

I am continuing, but I think the query is non-sensical. In one place, the query says that q2.daynum > q3.daynum . In another, that q2.daynum = q3.daynum . Hence, the query will not return anything. But, you can still express it as valid SQL. Notice that q3 is not really needed in the outer query. 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
                  )  ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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