Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4270792
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:21:16+00:00 2026-05-21T07:21:16+00:00

Hey everyone. I’m having a bit of trouble running a query / php combination

  • 0

Hey everyone. I’m having a bit of trouble running a query / php combination efficiently. I seem to be just looping over too many result sets in inner loops in my php. I’m sure there is a more efficient way of doing this. Any help very much appreciated.

I’ve got a table that holds 3500 recipes ([recipe]):
rid | recipe_name

And another table that holds 600 different ingredients ([ingredients])
iid | i_name

Each recipe has x number of ingredients associated to it, and I use a nice joining table to create the association ([recipe_ingredients])
uid | rid | iid
(where uid is just a unique id for the table)

For example:

rid: 1 | recipe_name: Lemon Tart
.....
iid: 99 | i_name: lemon curd
iid: 154 | i_name: flour
.....
1 | 1 | 99
2 | 1 | 154

The query I’m trying to run, allows the user to enter what ingredients they have, and it will tell you anything you can make with those ingredients. It doesn;t have to use all ingredients, but you do need to have all the ingredients for the recipe.

For instance if I had flour, egg, salt, milk and lemon curd I could make ‘Pancakes’, and ‘Lemon Tart’ (if we assume lemon tart has no other ingredients:)), but couldn’t make ‘Risotto’ (as I didnt have any rice, or anything else thats needed in it).

In my PHP I have an array containing all the ingredients the user has. At the moment they way I’m running this is going through every recipe (loop 1) and then checking all ingredients in that recipe to see if each ingredient is contained in my ingredients array (loop 2). As soon as it finds an ingredient in the recipe, that isnt in my array, it says “no” and goes onto the next recipe. If it does, it stores the rid in a new array, that I use later to display the results.

But if we look at the efficiency of that, if I assume 3500 recipes, and Ive got 40 ingredients in my array, the worst case scenario is it running through 3500 x 40n, where n = number of ingredients in the recipe. The best case is still 3500 x 40 (doesn’t find an ingredient first time for every recipe so exits).

I think my whole approach to this is wrong, and I think there must be some clever sql that I’m missing here. Any thoughts? I can always build up an sql statement from the ingredient array I have ……

Thanks a lot in advance, much appreciated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T07:21:16+00:00Added an answer on May 21, 2026 at 7:21 am

    I’d suggest storing the count of the number of ingredients for the recipe in the recipe table, just for efficiency’s sake (it will make the query quicker if it doesn’t have to calculate this information every time). This is denormalization, which is bad for data integrity but good for performance. You should be aware that this can cause data inconsistencies if recipes are updated and you are not careful to make sure the number is updated in every relevant place. I’ve assumed you’ve done this with the new column set as ing_count in the recipe table.

    Make sure you escape the values in for NAME1, NAME2, etc if they are provided via user input – otherwise you are at risk for SQL injection.

    select recipe.rid, recipe.recipe_name, recipe.ing_count, count(ri) as ing_match_count
    from recipe_ingredients ri 
    inner join (select iid from ingredients where i.name='NAME1' or i.name='NAME2' or i.NAME='NAME3') ing
    on ri.iid = ing.iid
    inner join recipe 
    on recipe.rid = ri.rid
    group by recipe.rid, recipe.recipe_name, recipe.ing_count
    having ing_match_count = recipe.ing_count
    

    If you don’t want to store the recipe count, you could do something like this:

    select recipe.rid, recipe.recipe_name, count(*) as ing_count, count(ing.iid) as ing_match_count
    from recipe_ingredients ri 
    inner join (select iid from ingredients where i.name='NAME1' or i.name='NAME2' or i.NAME='NAME3') ing
    on ri.iid = ing.iid
    right outer join recipe 
    on recipe.rid = ri.rid
    group by recipe.rid, recipe.recipe_name
    having ing_match_count = ing_count
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey everyone! I am having trouble with understanding modules -- I have two files,
Hey everyone, I'm working on a PHP application that needs to parse a .tpl
Hey everyone, I'm using Virtual PC and working with a virtual hard disk (*.vhd)
Hey everyone, I am trying to run the following program, but am getting a
Hey everyone. I'm trying to make a swing GUI with a button and a
Hey everyone, I'm working on a widget for Apple's Dashboard and I've run into
Hey everyone, I'm back and looking forward to more of your brilliance. I have
Hey everyone. The following is the code that I am using to play random
Hey everyone, I am getting a heap corruption error I cannot figure out. char
Hey everyone. My friend and I are re-learning Visual Basic, and we are stumped

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.