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 930639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:22:12+00:00 2026-05-15T20:22:12+00:00

recipe_category cid | category 1 | desserts 2 | cakes 3 | biscuits recipe_name

  • 0

recipe_category

cid | category
 1  | desserts
 2  | cakes
 3  | biscuits

recipe_name

id | recipe_name       | cid | iid
 1 | black forest cake | 1,2 | 1,2,3,4
 2 | angel cake        | 2   | 1,2,4
 3 | melting moments   | 3   | 2,5
 4 | croquembouche     | 1,3 | 1,5

ingredients

iid | ingredient_name
 1  | self-raising flour
 2  | milk
 3  | chocolate
 4  | baking powder
 5  | plain flour

I am able to query the DB using cid to pull certain recipes, ie. desserts:

SELECT * FROM recipe_name WHERE cid='1'

But then how do I create a list of ingredients like the below where ingredients are listed with <br>?

Black Forest Cake:
Self-Raising Flour
Milk
Chocolate
Baking Powder

I’m new to this, so please forgive any stupid questions!

  • 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-15T20:22:13+00:00Added an answer on May 15, 2026 at 8:22 pm

    Storing multivalued attributes in a single comma separated field is almost always a bad idea. It makes everything very difficult to query.

    Instead, you may want to consider refactoring your schema, using two new intersection tables.

    These two tables remain as they are (just changed name of recipe_category to categories in order not to clash with the intersection table):

    CREATE TABLE categories (
       cid int NOT NULL PRIMARY KEY,
       category_name varchar(50)
    ) ENGINE=INNODB;
    
    CREATE TABLE ingredients (
       iid int NOT NULL PRIMARY KEY,
       ingredient_name varchar(50)
    ) ENGINE=INNODB;
    

    Modify the recipe_name table as follows, removing the cid and iid fields:

    CREATE TABLE recipe_name (
        id int NOT NULL PRIMARY KEY,
        recipe_name varchar(50)
    ) ENGINE=INNODB;
    

    Then you can define your multivalued relationships using the following two intersection tables:

    CREATE TABLE recipe_ingredients (
        recipe_id int NOT NULL,
        ingredient_id int NOT NULL,
        PRIMARY KEY (recipe_id, ingredient_id),
        FOREIGN KEY (recipe_id) REFERENCES recipe_name (id),
        FOREIGN KEY (ingredient_id) REFERENCES ingredients (iid)
    ) ENGINE=INNODB;
    
    CREATE TABLE recipe_categories (
        recipe_id int NOT NULL,
        category_id int NOT NULL,
        PRIMARY KEY (recipe_id, category_id),
        FOREIGN KEY (recipe_id) REFERENCES recipe_name (id),
        FOREIGN KEY (category_id) REFERENCES categories (cid)
    ) ENGINE=INNODB;
    

    Now let’s populate these tables with your example data:

    INSERT INTO categories VALUES (1, 'desserts');
    INSERT INTO categories VALUES (2, 'cakes');
    INSERT INTO categories VALUES (3, 'biscuits');
    
    INSERT INTO ingredients VALUES(1, 'self-raising flour');
    INSERT INTO ingredients VALUES(2, 'milk');
    INSERT INTO ingredients VALUES(3, 'chocolate');
    INSERT INTO ingredients VALUES(4, 'baking powder');
    INSERT INTO ingredients VALUES(5, 'plain flour');
    
    INSERT INTO recipe_name VALUES(1, 'black forest cake');
    INSERT INTO recipe_name VALUES(2, 'angel cake');
    INSERT INTO recipe_name VALUES(3, 'melting moments');
    INSERT INTO recipe_name VALUES(4, 'croquembouche'); 
    

    To define the relationships between the recipes and their ingredients and categories, you would need to fill up the intersection tables as follows:

    INSERT INTO recipe_categories VALUES (1, 1);
    INSERT INTO recipe_categories VALUES (1, 2);
    INSERT INTO recipe_categories VALUES (2, 2);
    INSERT INTO recipe_categories VALUES (3, 3);
    INSERT INTO recipe_categories VALUES (4, 1);
    INSERT INTO recipe_categories VALUES (4, 3);
    
    INSERT INTO recipe_ingredients VALUES (1, 1);
    INSERT INTO recipe_ingredients VALUES (1, 2);
    INSERT INTO recipe_ingredients VALUES (1, 3);
    INSERT INTO recipe_ingredients VALUES (1, 4);
    INSERT INTO recipe_ingredients VALUES (2, 1);
    INSERT INTO recipe_ingredients VALUES (2, 2);
    INSERT INTO recipe_ingredients VALUES (2, 3);
    INSERT INTO recipe_ingredients VALUES (3, 2);
    INSERT INTO recipe_ingredients VALUES (3, 5);
    INSERT INTO recipe_ingredients VALUES (4, 1);
    INSERT INTO recipe_ingredients VALUES (4, 5);
    

    Finally, building your query will be as easy as this:

    SELECT i.ingredient_name
    FROM   recipe_ingredients ri
    JOIN   ingredients i ON (i.iid = ri.ingredient_id)
    WHERE  ri.recipe_id = (SELECT id 
                           FROM   recipe_name 
                           WHERE  recipe_name = 'Black Forest Cake');
    

    Result:

    +--------------------+
    | ingredient_name    |
    +--------------------+
    | self-raising flour |
    | milk               |
    | chocolate          |
    | baking powder      |
    +--------------------+
    4 rows in set (0.00 sec)
    

    You may then want to format that result set (adding the <br>s) in your application code instead of in SQL.

    However if you really wish to do that in SQL, then MySQL supports the handy GROUP_CONCAT() function, which can be used as follows:

    SELECT GROUP_CONCAT(i.ingredient_name separator '<BR>') output
    FROM   recipe_ingredients ri
    JOIN   ingredients i ON (i.iid = ri.ingredient_id)
    WHERE  ri.recipe_id = (SELECT id 
                           FROM   recipe_name 
                           WHERE  recipe_name = 'Black Forest Cake');
    

    Result:

    +----------------------------------------------------------+
    | output                                                   |
    +----------------------------------------------------------+
    | self-raising flour<BR>milk<BR>chocolate<BR>baking powder |
    +----------------------------------------------------------+
    1 row in set (0.00 sec)
    

    Dump that into HTML, and you’re good to go!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The recipe db continues... categories cid | category_name 1 | desserts 2 | cakes
ruby 1.9.2p290 rails 3.1.1 I have two models: RECIPE and RECIPE_CATEGORY with belongs_to and
So I have data in my database & the value of category is set
I've got some data in a table that looks like so: Recipe | Category
I have a table called food. I am selecting the category of the food
I've written a model for Category. The requirements here are that each category can
So say I have this XML file: <?xml version=1.0 encoding=utf-8 standalone=yes?> <Root> <Category Name=Tasties>
I have a problem checking a select that look like this: <select name=recipe[category]> <option
I'd like to implement a simple category system for my recipes. Here's my Recipe
I'm attempting to create a formset for the following models: class Category(models.Model): name =

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.