2012-10-16 192 views
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查询。也希望从子查询表中获取一些数据。从子查询中选择数据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 
); 

如果我有选择的外部查询的添加它们

查询提供了错误“未知列”,这有一定道理。

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

+1

显示失​​败查询 - 我们可以建议更好 – BugFinder

+0

补充说,失败的查询。 – amitchhajer

回答

3

重写查询与联接:

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 
+0

做了诡计,非常感谢。第二个更清洁。 – amitchhajer

1

t2在该点不可用。你应该为此使用连接。使用t1.column1 = t2.column2应该这样做。

+0

没错,那是我遇到的问题,正在寻找解决方法。 – amitchhajer