3
select t1.table1 from table1 as t1
where t1.column1 
in 
(
    select t2.column2 from table2 as t2 
    join 
    table3 as t3  on t2.column1=t3.column1 
    where t3.columnx=5
);

以上是我正在触发的 mysql 查询。还想要子查询表中的一些数据。

例如说表 t2 中的 columnxy。

失败的查询

select t1.table1,t2.columnxy from table1 as t1
where t1.column1 
in 
(
    select t2.column2 from table2 as t2 
    join 
    table3 as t3  on t2.column1=t3.column1 
    where t3.columnx=5
);

如果我使用外部查询的选择添加它们会给出错误“未知列”,这确实有意义。

是正确的方法还是应该用连接重写查询?

4

2 回答 2

4

用连接重写查询:

SELECT t1.table1, t.columnxy
FROM   table1 AS t1 JOIN (
  SELECT t2.column2, t2.columnxy
  FROM   table2 AS t2 JOIN table3 AS t3 USING (column1)
  WHERE  t3.columnx = 5
) t ON t1.column1 = t.column2

或者:

SELECT t1.table1, t2.columnxy
FROM   table1 AS t1
  JOIN table2 AS t2 ON t1.column1 = t2.column2
  JOIN table3 AS t3 ON t2.column1 = t3.column1
WHERE  t3.columnx = 5
于 2012-10-16T07:10:58.413 回答
2

此时 t2 不可用。您应该为此使用连接。使用 t1.column1=t2.column2 应该可以做到。

于 2012-10-16T07:10:50.227 回答