Many to many relationship in SQL | Sololearn: Learn to code for FREE!

+1

Many to many relationship in SQL

I have two tables, one called shops and another called products both are joined with table called shops_products. Here I want to select all products with a given shop id from shops. how could I write a SQL?

5/8/2017 7:09:34 PM

Joas Dioniz

2 Answers

New Answer

+1

SELECT * FROM shops_products JOIN products ON (shops_products.product_id = products.id) WHERE shops_products.shop_id = 1

+1

SELECT * FROM Products WHERE Product_ID IN ( SELECT Product_ID FROM SHOPS_PRODUTS WHERE Shop_ID = 1 )