0

In my MySQL database I have the following tables:

recipes (=ricette) which contains the id of the recipe and some basic info

ingredients (=ingredienti) which as got as many rows as many ingredients there are in that recipe, and finally the table

recipe_kind (=tiporicetta) which contains the id of the recipe and the kind of the recipe.

I would like to get all the ingredients of a recipe and so I perform:

SELECT DISTINCT
    r.nome,
    r.descrizione,
    r.persone,
    t.tipo,
    c.cibo,
    i.quantita,
    i.unitamisura_id
FROM
    ricette AS r
        LEFT JOIN
    ingredienti AS i ON i.ricette_id = r.id
        LEFT JOIN
    cibo AS c ON c.id = i.cibo_id
        LEFT JOIN
    tiporicetta AS t ON t.id = r.tiporicetta_id
WHERE
    (r.id = '52')

but with that query I get the name, description, people and kind many times, once for each row. Instead, I would like to have nome, descrizione, persone and tipo only once and then the list of the ingredients. How can I do that with an unique query?

4
  • Can you make a sqlfiddle? Commented Mar 16, 2016 at 12:50
  • 2
    Do a GROUP BY, with group_concat. Commented Mar 16, 2016 at 12:52
  • 2
    To my way of thinking, and to a rough approximation, there is NO problem in MySQL for which GROUP_CONCAT is the solution. Instead, if you like, consider following this simple two-step course of action: 1. If you have not already done so, provide proper DDLs (and/or an sqlfiddle) so that we can more easily replicate the problem. 2. If you have not already done so, provide a desired result set that corresponds with the information provided in step 1. Commented Mar 16, 2016 at 12:52
  • You're trying to solve a display issue in SQL. Instead, let your display tier handle it. Commented Mar 16, 2016 at 13:22

1 Answer 1

1

Try this:

SELECT 
    r.nome,
    r.descrizione,
    r.persone,
    "", "", "", ""
FROM ricette AS r
WHERE 
    (r.id = '52')

UNION

SELECT
    "", "", ""
    t.tipo,
    c.cibo,
    i.quantita,
    i.unitamisura_id
FROM
    ricette AS r LEFT JOIN
    ingredienti AS i ON i.ricette_id = r.id LEFT JOIN
    cibo AS c ON c.id = i.cibo_id LEFT JOIN
    tiporicetta AS t ON t.id = r.tiporicetta_id
WHERE
    (r.id = '52')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.