Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL pattern to get "and" list of multiple-row matches?
    primarykey
    data
    text
    <p>I'm not a database programmer, but I have a simple database-backed app where I have items with tags. Each item may have multiple tags, so I'm using a typical junction table (like <a href="https://stackoverflow.com/questions/444251/how-to-store-a-list-in-a-db-column">this</a>), where each row represents the fact that the item with the appropriate ID has the tag with the appropriate ID.</p> <p>This works very logically when I want to do something like select all items with a given tag.</p> <p>But, <strong>what is the typical pattern for doing <code>AND</code> searches?</strong> That is, what if I want to find all items which have all of a certain set of tags? This is such a common operation that I'd think some of the intro tutorials would cover it, but I guess I'm not looking in the right places.</p> <p>The approach I tried was to use <code>INTERSECT</code>, first directly and then with subqueries and <code>IN</code>. This works, but builds up long-seeming queries quickly as I add search terms. And, crucially, this approach appears to be about an order of magnitude slower than the approach of shoving all the tags as text into one "tags" column and using SQLite's full-text search. (And, as I would expect/hope, the FTS search gets faster as I add more terms, which doesn't seem to be the case with the INTERSECTS approach.)</p> <p>What's the proper design pattern here, and what's the right way to make it snappy? I'm using SQLite in this case, but I'm most interested in a general answer, since this <em>must</em> be a common thing to do.</p>
    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.
 

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