Mysql 在where子句中选择,访问当前父选择列

Mysql 在where子句中选择,访问当前父选择列,mysql,sql,subquery,Mysql,Sql,Subquery,我有一个查询,它在where子句中包含一个select语句。我现在的问题是,我如何访问家长的select数据 例如: select * from TABLE_1 as t1 INNER JOIN TABLE_2 as t2 where (... and ...) OR (not exists(select * from TABLE_3 as t3 inner join TABLE_1 ON t3.t1_id = t1

我有一个查询,它在where子句中包含一个
select
语句。我现在的问题是,我如何访问家长的
select
数据

例如:

select * from TABLE_1 as t1 INNER JOIN TABLE_2 as t2
where (... and ...) OR 
      (not exists(select * from TABLE_3 as t3 
                                inner join TABLE_1 ON t3.t1_id = t1.id
最后一行是发生错误的地方:
t1.id不是列

如何从表t1中访问当前值?
我使用的是MySql 5.1

首先,您需要声明您将
加入表1上的表2

SELECT
  *
FROM
  TABLE_1 as t1 
  INNER JOIN TABLE_2 as t2 ON
    t2.PK = t1.FK --Whatever your keys are
WHERE
  (... and ...)
  OR
  (
  NOT EXISTS (select * from TABLE_3 as t3 WHERE t3.t1_id = t1.id)
  )
SELECT *
FROM TABLE_1 AS t1 
INNER JOIN TABLE_2 AS t2 ON t2.t1_id = t1.id
t1.id=t2.t1\u id
只是一个例子,您需要决定希望加入哪些列。然后在
WHERE
子句中,您不需要再次对表1进行
内部联接,因为您已经从中进行了选择

SELECT *
FROM TABLE_1 AS t1 
INNER JOIN TABLE_2 AS t2 ON t2.t1_id = t1.id
WHERE (... AND ...) OR 
(
    NOT EXISTS
    (
        SELECT * 
        FROM TABLE_3 AS t3 
        WHERE t3.t1_id = t1.id
    )
)

您可以发布完整的查询吗?您需要将内部表_1设置为t4,并在t3.t1_id=t4.id上设置以下条件,其中t4.id=t1.idI向表_2的联接添加了一个ON子句,因为您的示例中缺少该子句。您的解决方案是,检查表3中是否存在行的相关子查询没有正确编写。您只需通过t1别名使用列,而不是尝试重新连接到表_1。