php - MySQL Multiple WHERE clauses to same column -


i have table of users information in 1-to-1 relationship...

    users table      ------------------------     id        |   name     ------------------------     800       |  bob smith     801       |  jane doe     802       |  fred flintstone     803       |  barney rubble     804       |  marge simpson 

i have many-to-1 relationship table houses usergroup mapping of users group(s) part of

    user_usergroup_map table      ------------------------     user_id   |   group_id     ------------------------     800       |  16     800       |  27     801       |  25     801       |  27     802       |  17     802       |  19     802       |  22     802       |  25     803       |  25     803       |  27     804       |  15 

i trying generate array of distinct users part of 2 distinct groups. @ same time, trying keep within single array (for using pagination reference).

i have been able query both tables via join statement...

    $group_id1 = 25;     $group_id2 = 27;      $query = select distinct name, id users inner join user_usergroup_map on user_usergroup_map.user_id=users.id group_id in('$group_id1', '$group_id2'); 

however, this, distinct list of users part of either group_id 15 or group_id 27, instead of part of both groups. know i've been staring @ far long, , i'm missing simplistic logical element, can't see it, life of me. if out there shed light on i'm doing wrong, i'd appreciate it.

assuming have constraint on user_usergroup_map table such given user cannot assigned same group_id twice, can this:

select ... users id in (             select user_id             user_usergroup_map             group_id in(25,27)             group user_id             having count(*) = 2             ) 

Comments