Не могу присоединиться к столу, если он выбран дважды - PullRequest
0 голосов
/ 01 октября 2018

Я пытаюсь построить запрос в 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

Почему это может быть?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...