mysql - What does this sql mean with not exists? -


a table this

int char int  int id  name a_id b_id  select count(*) tbl t1 b_id = 12 , not exists(select * tbl t2 t2.a_id = t1.b_id , t2.b_id = t1.a_id) 

i think @ least equals

select count(*) tbl t1 b_id = 12 , not exists(select * tbl t2 t2.a_id = 12 , t2.b_id = t1.a_id) 

then mean?

for example, select a_id tbl t1 b_id = 12 gives 1,2,3,4

then following:

select * tbl t2 t2.a_id = 12 , t2.b_id = 1 # null select * tbl t2 t2.a_id = 12 , t2.b_id = 2 # exists select * tbl t2 t2.a_id = 12 , t2.b_id = 3 # exists select * tbl t2 t2.a_id = 12 , t2.b_id = 4 # null 

so count(*) 2?

this 1 interesting because finds "symmetries" in a_id , b_id number 12,so counts number of rows in table b_id=12 except ones have symmetric pair, this:

| a_id | b_id | --------------- |  12  |  12  | |  12  |  2   | |  2   |  12  | 

or more speaking, query skips counting rows have a_id=x b_id=12 , there exists row a_id=12 , b_12=x.

so not exists there skip counting rows have symmetric pair, don't know can useful in applications interesting nonetheless.


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 -