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
);

Above is the mysql query i am firing. Wanted some data from the subquery tables also.

For example say columnxy from table t2.

query that fails

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
);

If i add them with select of the outer query gives error "unknown column" which does make sense.

Is the right way or should rewrite query with joins?

2
  • 1
    Show the query that failed - we can advise better Commented Oct 16, 2012 at 7:08
  • added the query that is failing. Commented Oct 16, 2012 at 7:10

2 Answers 2

4

Rewrite the query with joins:

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

Or:

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
Sign up to request clarification or add additional context in comments.

Comments

2

The t2 is not available at that point. You should use a join for this. Using t1.column1=t2.column2 should do it.

1 Comment

right, that's the issue i am having, searching for the workaround.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.