0

I'm currently doing this query, however, I'm always getting a result where there should be none, because there's a corresponding row.

select `likes`.*, `likeshistory`.`like_id`, `likeshistory`.`instagram_id` from `likes` join `likeshistory` on `likes`.`id` = `likeshistory`.`like_id` where (`finished` = '0' and `active` = '1' and `banned` = '0') and `instagram_id` not in ('2') limit 1

Basically I want to select one "likes" that has no "likeshistory" for (instagram_id = 2)

likeshistory structure

Thank you! William

William B
  • 53
  • 1
  • 1
  • 8

1 Answers1

0

You can use not exists. The purpose of the query is clearer:

select `likes`.*, `likeshistory`.`like_id`, `likeshistory`.`instagram_id`
from `likes` l
where not exists (select 1
                  from `likeshistory` lh
                  where l.id = lh.`like_id` and
                        (`finished` = '0' and `active` = '1' and `banned` = '0') and lh.instagram_id not in ('2')
                 );

If any of the conditions in the subquery (except the correlation clause) are on likes, then those should be in the outer query.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786