Question

I'm not used to MySql (or any SQL at all) but for a project, I need to get a value from an association table.

Let's say I have these tables :

tbl_products
fld_id = 4

tbl_colors
fld_name = 'Black'
fld_id = 1

tbl_product_colors
fk_prod_id = 4
fk_color_id = 1

What I need to do is, based on a product id (4), I want the name of the color associated to the product. I did things like this :

SELECT fld_name FROM tbl_colors 
INNER JOIN tbl_products 
ON tbl_products.fld_id = tbl_product_colors.fk_prod_id 
WHERE tbl_products.fld_id = 4

Any help would be very appreciated.

Thank you

Was it helpful?

Solution

You can do this:

SELECT fld_name
FROM (tbl_product_colors JOIN tbl_product 
ON tbl_product_colors.fk_prod_id = tbl_product.fld_id) JOIN 
tbl_colors ON tbl_product_colors.fk_color_id = tbl_colors.fld_id
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top