mysql - Issue with Sql query: count distinct row values relative to other row value -


i have following table:

name        category    posts ------------------------------ client    01          5348 client    05          2584 client b    02          105 client c    14          10558 client c    16          511 client d    01          4863 client d    01          1823 

now i'd select rows where:

  • the client present in 1 category. means both client b , client d still possible.

and

  • this category "01". in end desired output be:

    client d    01          4863 client d    01          1823 

the query had in mind:

select name, category, posts exampletable (count number of present distinct categories each name = '1' , category='01'); 

the problem don't know how translate "count number of present distinct categories each name" correct sql code. can me out this?

you can use sub-query achieve this:

select e.name, e.category, e.posts exampletable e e.name not in (select e1.name exampletable e1 e.name = e1.name , e.category <> e1.category)  , e.category = '01' 

explanation: subquery return names do not have one-is-to-one relation category, can eliminated using not in , further filter can applied using and category = '01'


Comments