Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can find the recipes for which the user has all ingredients by using an outer join. If a given ingredient is not found in the user's supply, <code>ui.*</code> will be NULL. </p> <p>Then count the ingredients for the drink, compare to the count of matching ingredients in the user's supply (NULLs don't count), and if the counts are equal, then all the ingredients are matched by the user's supply.</p> <pre><code>SELECT d.* FROM drinks d INNER JOIN drink_ingredient di ON d.id = di.drink LEFT OUTER JOIN user_ingredients ui ON di.ingredient = ui.ingredient AND ui.user = :user GROUP BY d.id HAVING COUNT(di.ingredient) = COUNT(ui.ingredient); </code></pre> <p>Even if the user has additional ingredients that are not part of this drink's recipe, I assume that's okay, those ingredients will just stay in the cupboard for next time. :-)</p> <p>Matching a user's ingredient input to the canonical row in the <code>ingredients</code> table is another matter. That is, populating the <code>user_ingredients</code> table so the numeric foreign keys reference the correct row. You can do that during the user's data entry where they list the ingredients they have.</p> <pre><code>INSERT INTO user_ingredients (user, ingredient) SELECT :user, i.id FROM ingredients i WHERE :ingredient REGEXP CONCAT('[[:&lt;:]]', i.name, '[[:&gt;:]]'); </code></pre> <p>So the user's input <code>'smirnoff vodka'</code> matches the regular expression <code>'[[:&lt;:]]vodka[[:&gt;:]]'</code></p> <p>Note that a substring match like this is hard to optimize. It'll have to run a full table-scan. But I assume you only have a few hundred rows in the ingredients table, so it won't be too bad. Otherwise, you'd have to use a fulltext indexing solution.</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. 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