Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming you have a <code>users</code> table as well:</p> <pre><code>SELECT id FROM conversations AS c WHERE NOT EXISTS ( SELECT * FROM users AS u WHERE u.id IN (1, 2, 3, 4) AND NOT EXISTS ( SELECT * FROM conversations_users AS cu WHERE cu.user = u.id AND cu.conversation = c.id ) ) AND NOT EXISTS ( SELECT * FROM conversations_users AS co -- and only them WHERE co.conversation = c.id AND co.user NOT IN (1, 2, 3, 4) ) ; </code></pre> <p>If you don't have a <code>users</code> table or you don't like to use it (can'see why but anyway), you can replace this part:</p> <pre><code>WHERE NOT EXISTS ( SELECT * FROM users AS u WHERE u.id IN (1, 2, 3, 4) AND NOT EXISTS </code></pre> <p>with:</p> <pre><code>WHERE NOT EXISTS ( SELECT * FROM (SELECT 1 AS id UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS u WHERE NOT EXISTS </code></pre> <hr> <p>The above query, while being general will not be very efficient in MySQL (blame the double nesting and the naive optimizer). The <code>GROUP BY / COUNT</code> way is probably a level more efficient - but please test with your data. You can also find many more ways (more than 10) to answer this kind of question, in this answer: <strong><a href="https://stackoverflow.com/questions/7364969/how-to-filter-sql-results-in-a-has-many-through-relation">How to filter SQL results in a has-many-through relation</a></strong> Some of them do not work in MySQL but many do. I'd expect queries 5 and 6 there to be quite efficient in MySQL (a level more efficient than the group by queries). </p> <p>Your case has a difference, you want exact relational division while that question/answers are about (simple) relational division, so you could write 5 like this:</p> <pre><code>SELECT id FROM conversations AS c WHERE EXISTS (SELECT * FROM conversations_users AS cu WHERE cu.conversation = c.id AND cu.user = 1) AND EXISTS (SELECT * FROM conversations_users AS cu WHERE cu.conversation = c.id AND cu.user = 2) AND EXISTS (SELECT * FROM conversations_users AS cu WHERE cu.conversation = c.id AND cu.user = 3) AND EXISTS (SELECT * FROM conversations_users AS cu WHERE cu.conversation = c.id AND cu.user = 4) AND NOT EXISTS (SELECT * FROM conversations_users AS cu WHERE cu.conversation = c.id AND cu.user NOT IN (1,2,3,4)) </code></pre>
    singulars
    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.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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