SQL query: how to translate IN() into a JOIN?
Posted
by tangens
on Stack Overflow
See other posts from Stack Overflow
or by tangens
Published on 2010-04-06T14:17:44Z
Indexed on
2010/04/06
14:23 UTC
Read the original article
Hit count: 242
I have a lot of SQL queries like this:
SELECT o.Id, o.attrib1, o.attrib2 FROM table1 o WHERE o.Id IN (
SELECT DISTINCT Id FROM table1, table2, table3 WHERE ...
)
These queries have to run on different database engines (MySql, Oracle, DB2, MS-Sql, Hypersonic), so I can only use common SQL syntax.
Here I read, that with MySql the IN
statement isn't optimized and it's really slow, so I want to switch this into a JOIN
.
I tried:
SELECT o.Id, o.attrib1, o.attrib2 FROM table1 o, table2, table3 WHERE ...
But this does not take into account the DISTINCT
keyword.
Question: How do I get rid of the duplicate rows using the JOIN
approach?
© Stack Overflow or respective owner