3

How can I optimize this mysql query? I'm using the IN() operator, but I believe it's not the way to do it.

select * 
from users 
where usid NOT in 
(
select usid 
from images 
where status=0
) 
and us_status=0
1
  • This question and more importantly this link in the accepted answer should help you out here. In summary, for MySQL use LEFT JOIN/IS NULL to remove matching records. Commented Sep 11, 2015 at 9:04

3 Answers 3

4

Using a LEFT OUTER JOIN

SELECT users.* 
FROM users 
LEFT OUTER JOIN images ON user.usid = images.usid AND images.status = 0
WHERE images.usid IS NULL
AND us_status = 0

This avoids using IN which can perform poorly.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks very much, just instead us_status must be users.status yes?
Your original query had us_status in it. If that was a typo for user.status then change it to that
0

You can use the following query :

SELECT us.* 
FROM users  as us
INNER JOIN images as img ON us.usid = img.usid AND img.status=1
WHERE us.us_status = 0

Must read the article : http://www.w3schools.com/sql/sql_join.asp

Comments

0
SELECT users.* 
FROM users 
LEFT JOIN images ON users.usid = images.usid AND images.status=1 AND images.usid IS NOT NULL
WHERE users.us_status = 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.