title: Having Trouble Writing SQL query Many To Many Relationship
body: <p>I have a Django application with the following postgres db tables: Publication and Tag</p>
  
  <pre><code>Publication {
      title
      tags
  }
  
  Tag {
      title
  }
  </code></pre>
  
  <p>Tag and Publication have a many to many relationship.  </p>
  
  <p>What I want to do is do an and/or combo search: for example, let's say I have four tags: A, B, C, D. I want to find all publications that have an A OR B tag associated with it, AND a C OR D tag associated with it. For example, in a search field I might enter: (A || B) &amp;&amp; (C || D). In other words, the results should yield publications that have at least one tag from each 'or' pair. </p>
  
  <p>I'd like to be able to enter the tag titles and return the publication titles.</p>
  
  <p>I tried doing this using a Django filter, and even asked this question yesterday: <a href="https://stackoverflow.com/questions/16965942/what-is-an-efficient-way-to-do-an-and-or-search-django-postgres-app/16967864#16967864">What is an Efficient Way to do an AND/OR Search Django-Postgres App?</a> and finally realized that doing a raw sequel query was probably better. The only problem: I've never written a raw sequel statement.</p>
  
  <p>I just spent the last couple of hours looking at this question: <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>, and trying to apply it to my circumstance, but I'm too much of a newbie to be able to do it. </p>
  
  <p>Given my models, can anyone show me how to construct this query?</p>