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
);
如果我用选择外部查询添加它们会给出错误“未知列”,这确实有意义。
是正确的方法还是应该用连接重写查询?
答案 0 :(得分: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
答案 1 :(得分:2)
此时t2不可用。您应该使用联接。使用t1.column1 = t2.column2应该这样做。