简体   繁体   中英

Transforming subquery to INNER JOIN

Transforming the following query (with subquery)

SELECT * 
FROM t1
WHERE t1.a IN (SELECT t2.b FROM t2 where t2.c = 1)

.. to this query with INNER JOIN

SELECT *
FROM t1
INNER JOIN t2
ON t1.a = t2.b AND t2.c = 1

I can get a nice performance boost (learned at http://spin.atomicobject.com/2011/03/25/mysql-in-query-performance )

But how about a query like this:

SELECT * 
FROM t1
WHERE t1.a NOT IN (SELECT t2.b FROM t2 where t2.c <> 1)

Note for the "NOT IN" and "<>". Can I achieve something similar?

Try this:

SELECT *
FROM t1
LEFT OUTER JOIN t2
ON t1.a = t2.b
WHERE t2.b IS NULL OR t2.c = 1

EDIT: I accidentally wrote INNER join. Sorry; it should be LEFT OUTER join.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM