Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It would be easier to understand your problem if you post the table structure and some sample data. But here are a few suggestions based on your current query that you might be able to use.</p> <p>These queries reduce the number of subqueries that you are using. One of the obvious changes is the difference in the way it gets the list of <code>user_id</code>'s with each group:</p> <pre><code>select user_id from group_user_map where group_id in (2601,119) union all select user_id from group_user_map where group_id in (58,226) union all select user_id from group_user_map where group_id in (1299,525) union all select user_id from group_user_map where group_id in (2524,128); </code></pre> <p>This uses a <code>UNION ALL</code> which will list all of the <code>user_id</code> even if they are duplicated. Once you have this list of <code>user_id</code>'s then you get the <code>count</code> by applying a <code>count(distinct user_id)</code> and use a <code>HAVING</code> clause to find those that have 4 occurrences. </p> <p>First, you could consolidate your current query to the following version in a <code>WHERE</code> clause:</p> <pre><code>select * from users_table where username IN (select user_id from ( select user_id from group_user_map where group_id in (2601,119) union all select user_id from group_user_map where group_id in (58,226) union all select user_id from group_user_map where group_id in (1299,525) union all select user_id from group_user_map where group_id in (2524,128) ) thegroups where user_id not in (select user_id from group_user_map where group_id in (2572)) group by userid having count(distinct userid) = 4); </code></pre> <p>Or you could use the query in the <code>WHERE</code> clause in a subquery that you JOIN to:</p> <pre><code>select ut.* from users_table ut inner join ( select user_id from ( select user_id from group_user_map where group_id in (2601,119) union all select user_id from group_user_map where group_id in (58,226) union all select user_id from group_user_map where group_id in (1299,525) union all select user_id from group_user_map where group_id in (2524,128) ) thegroups where user_id not in (select user_id from group_user_map where group_id in (2572)) group by userid having count(distinct userid) = 4 ) biggergroup on ut.username = biggergroup.user_id; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload