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

  • SEARCH
  • Home
  • 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 8125805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:56:13+00:00 2026-06-06T06:56:13+00:00

I find my self lost while trying to create an actually pretty easy SQL

  • 0

I find my self lost while trying to create an actually pretty easy SQL statement.

I got a database with 3 Tables:

  • recipes – storing some recipes names for cooking
  • ingredients_recipes – links ingredients with recipes
  • ingredients – the ingredients used by recipes.

its stored like this to make it double-clear:

recipes:

id | name
 1 | lasagne alla bolognese
 2 | tuna with tomatoes

ingredients:

id | name
 1 | lasagne slices
 2 | meat
 3 | tomato
 4 | tuna

and join-table ingredients_recipes:

ingredient_id | recipes_id
            1 | 1
            2 | 1
            3 | 1
            3 | 2
            4 | 2 

So as you can see, there are 2 really undelicios recipes which I should at least give some spices. But what I want to do before is selecting recipes by ingredients.

I want to have all my recipes having meat AND tomatoes:

SELECT recipes.name FROM recipes r
INNER JOIN ingredients_recipes ir ON ir.recipes_id = r.id
WHERE ir.ingredient_id IN ( 2 ) AND ir.ingredient_id IN ( 3 )

-> lasagna.. fine! (I use IN since there might be a bunch of ingredients, like “tomato”, “tomatoes”, “tomatoes, sliced” etc.. )

When I want to have e.g. all recipes having tomatoes but NOT having tuna, I tried:

SELECT recipes.name FROM recipes r
INNER JOIN ingredients_recipes ir ON ir.recipes_id = r.id
WHERE ir.ingredient_id IN ( 2 ) AND ir.ingredient_id NOT IN ( 4 )

-> still getting tuna – since one of the rows i join does not contain ingredient 4. okay :/

What I am wondering now is, what do I have to do, to get my desired results.
I currently put an arrow to my knee by doing a subselect like this:

SELECT recipes.name FROM recipes r
INNER JOIN ingredients_recipes ir ON ir.recipes_id = r.id
WHERE (
        ir.ingredient_id IN ( 2 ) -- or more..
        AND 
        recipes.id NOT IN ( SELECT recipes_id FROM ingredients_recipes
                             WHERE ingredient_id IN ( 4 ) -- actually i paste names of the ingredients.. but that is not the case. just to shorten the query.. by filling in a comment twice as long..
                            )
      )

as I am quite new to mySQL, I don’t even know what to google for. so any help in that case, and o/c the better SQL statement would be ..

 IN(awesome).

Edit: ah, and yes I actually do grouping.. 😉

  • 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-06-06T06:56:15+00:00Added an answer on June 6, 2026 at 6:56 am

    This is one way. (untested)

    SELECT r.name 
    FROM recipes r
    JOIN ingredients_recipes ir 
      ON ir.recipe_id = r.id
    WHERE EXISTS ( SELECT *
        FROM ingredients_recipes ex
        WHERE ex.recipe_id = r.recipe_id
        AND ex.ingredient_id IN ( 2 , 3 )
        GROUP BY nx.recipe_id
        HAVING COUNT(*) = 2
        );
    

    The only way for count(*) to be 2 is if 2 and 3 ar both present.

    Another way could be: (still untested)

    SELECT r.name 
    FROM recipes r
    JOIN ingredients_recipes ir 
      ON ir.recipe_id = r.id
    WHERE EXISTS ( SELECT *
        FROM ingredients_recipes ex
        WHERE ex.recipe_id = r.recipe_id
        AND ex.ingredient_id = 2
        )
    AND EXISTS ( SELECT *
        FROM ingredients_recipes ex
        WHERE ex.recipe_id = r.recipe_id
        AND ex.ingredient_id = 3
        );
    

    UPDATE: (i had misread the question) If you also don’t want recipes with particular ingredients present you could add yet another subquery “leg” to the query:

    ... AND NOT EXISTS ( SELECT *
        FROM ingredients_recipes ex
        WHERE ex.recipe_id = r.recipe_id
        AND ex.ingredient_id IN ( 4,5 )
        );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I find my self out of ideas trying to get this app to work.
I'm pretty new to WPF. I often find my self struggling with getting a
I find my self doing this type of IF statement allot. For example: if($variable
I find my self hard to design action enums for events. f.ex making calculation
I find my self in a situation where I have to choose between either
I find my self overriding Equals() and GetHashCode() frequently to implement the semantic that
I find the following example mildly surprising: >>> class Foo: def blah(self): pass >>>
Here is my one model.. CardSignup.rb def credit_status_on_create Organization.find(self.organization_id).update_credits end And here's my other
I Find my self using jquery a lot in visual studio for my current
#find the most recent news item for a specific locale def self.find_most_recent_news_for_locale first(:include =>

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.