search

Home  >  Q&A  >  body text

mysql - Should I use left join or not in in the following statement?

Implementation: Query records in a that are not associated with b

1、select id from a left join b on a.id = b.aid where b.id is null and status = 1
2、select id from a where id not in (select aid from b) and  status = 1

Which one is better, or if there is another way, please leave the answer

PHP中文网PHP中文网2793 days ago1092

reply all(2)I'll reply

  • 高洛峰

    高洛峰2017-06-06 09:54:24

    not existsCorrect answer

    select id from a where not exists (select 1 from b where a.id=b.aid) and status = 1

    reply
    0
  • 習慣沉默

    習慣沉默2017-06-06 09:54:24

    I want to use not exists

    A brief discussion on the difference between in and not in, exists and not exists in sql

    reply
    0
  • Cancelreply