Skip to content
Advertisement

Select based on multiple ID’s in the same column

I have two SQL tables already built (not by me) :

One table that has all the ingredients with ID’s assigned to them :

and the second table that has finished products that have reference to ingredientID in the ProductIngredients column but no the product name and they are built like this :

If they don’t have any reference then its written NULL, and if they have, then they start with a “;” symbol and every ingredient is divided by a “;” symbol.

My goal is to join those two tables and make a select statement that returns me, instead of the ID’s of the other column, the actual names of the used ingredients like this :

Can anyone help out with this?

Advertisement

Answer

You need a left join of Products to ProductIngredients and group by product:

The function group_concat() works in MySql but you can find similar functions to other databases.
See the demo.
Results:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement