Я пытаюсь построить запрос в MySQL, который выглядит следующим образом:
select
t1.id
,t1Next.code as nextCode
,t1Prev.code as prevCode
from
t1
,(select * from t2 where idMain in (idList)) as t2Main
,(select * from t2 where idAssoc in (idList)) as t2Assoc
inner join t1 as t1Next on t1Next.id = t2Main.idAssoc
inner join t1 as t1Prev on t1Prev.id = t2Assoc.idMain
where t1.id in (idList)
and t2Main.idMain = t1.id
and t2Assoc.idAssoc = t1.id
, который возвращается с ошибкой Unknown column 't2Main.idAssoc' in 'on clause'
.
Этот запрос работает:
select
t1.id
,t1Next.code as nextCode
from
t1
,(select * from t2 where idMain in (idList)) as t2Main
inner join t1 as t1Next on t1Next.id = t2Main.idAssoc
where t1.id in (idList)
and t2Main.idMain = t1.id
Добавление второго подзапроса, подобного этому, прерывает запрос и приводит к вышеприведенной ошибке:
select
t1.id
,t1Next.code as nextCode
from
t1
,(select * from t2 where idMain in (idList)) as t2Main
,(select * from t2 where idAssoc in (idList)) as t2Assoc
inner join t1 as t1Next on t1Next.id = t2Main.idAssoc
where t1.id in (idList)
and t2Main.idMain = t1.id
Почему это может быть?