Hi
Main Topics
Browse All TopicsHi,
The following is a simplified version of a complex query i wrote.
SELECT table2.x, table2.y
FROM table1
LEFT JOIN (SELECT table3.x, max(table3.y)
FROM table3
GROUP BY table3.x) AS table2
ON table1.x = table2.x
and it works... but it is slower since the subquery fetches whole table again and again.
Is there any way to make this faster by adding a condition inside subquery...
I want to do something like this.
SELECT table2.x, table2.y
FROM table1
LEFT JOIN (SELECT table3.x, max(table3.y)
FROM table3
WHERE table3.x = table1.x <<<<<<------------- does not work // Unknown table 'table1' in where clause
GROUP BY table3.x) AS table2
ON table1.x = table2.x
Any alternative query is welcome.
Thanks
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Yes use of table1 in subquery is suported for example in oracle but in mysql won't work
SELECT table2.x, table2.y
FROM table1
LEFT JOIN (SELECT table3.x, max(table3.y)
FROM table3
inner join table1 using (x)
GROUP BY table3.x) AS table2
ON table1.x = table2.x
wouldn't this speedup yur query if table 3 is really big and use the same where clouse in sub query and outside it?
Same result will produce:
SELECT table3.x, max(table3.y)
FROM table3
inner join table1 using (x)
GROUP BY table3.x
but meabye you were looking for something like this:
SELECT table2.x, table2.y
FROM table1
LEFT JOIN (SELECT table3.x, max(table3.y)
FROM table3
WHERE table3.x IN (SELECT table1.x FROM table1)
GROUP BY table3.x) AS table2
ON table1.x = table2.x
or to not call queries on table1 twice you could create temporary table with records from table1 you want to use
create temporary temp_table1
SELECT table1.x FROM table1
WHERE..........
then just:
SELECT table2.x, table2.y
FROM temp_table1
LEFT JOIN (SELECT table3.x, max(table3.y)
FROM table3
WHERE table3.x IN (SELECT temp_table1.x FROM temp_table1)
GROUP BY table3.x) AS table2
ON temp_table1.x = table2.x
Will this be quicker enough?
Business Accounts
Answer for Membership
by: aneeshattingalPosted on 2006-03-21 at 23:08:20ID: 16254823
I dont find any erorrs there, check whether 'Table1' already exists on your database