mysql - How to select count of records exist in table also exist in table b? -


tablea  id    a_id     b_id 1     100      200 2     101      201 3     100      202   tableb  id    a_id     b_id 2     100      200 3     201      101 4     203      100 

so want count of a_id=100 except 1 exist in tableb

so

in tablea got 2:     1     100      200     3     100      202 

but 100/200 exists in tableb, result 1

how write sql?

you can use not exists:

select      count(1)        tablea       not exists (                             select    null                                  tableb b                                 b.a_id = a.a_id                                   , b.b_id = a.b_id                        )        , a.a_id = 100 

if want left join:

select      count(1)         tablea left join   tableb b         on  b.a_id = a.a_id         , b.b_id = a.b_id       b.a_id null         , a.a_id = 100 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -