mysql - sql self join and many to many relationship -


i have tabled called category , record can have parent or child , category. category tables many-to-many relationship posts through post_category.

category[id, name category_id]  posts[id,title, body....]  post_cats[id,post_id,category_id] 

sample data

category id    name     category_id 1     xxxx      0 2     yyyy      0 3     zzzz      1 4     wwww      1 5     aaaa      2 6     bbbb      2    posts id    title              body 1     aaaaaaaaaa 2     bbbbbbbbbbbbbb 3     ccccccccccccccc 4     ddddddddddd  post_cats id  post_id       category_id 1    1                 3 2    1                 4 3    2                 5 4    2                 6 5    2                 3 

category record 1 has post of 3 , record 2 has post of 2 want count posts belongs category name 'xxxx' or want count posts belongs each parent category child category counted.

select post_id   post_category group post_id  having count(*) = (select count(*) category) 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -