Home > Net >  Is there a way where i can select 3 tables at once and do where x = 2
Is there a way where i can select 3 tables at once and do where x = 2

Time:12-15

So i'm basically working on a supermarket database and i have a task that is about seeing diferent details(price history,history of purchase of the product...) a product only searching for the product id. i did this on sql

SELECT* FROM price_history, product_purchase where product_id = 2,
SELECT* FROM product where id = 2,
SELECT* FROM product_purchase where product_id = 2;

Im i student so im not that good in sql rn so it would be great if someone could help :)

CodePudding user response:

you can do a single select on the 3 tables, and join them by their id

SELECT product.*, product_purchase.*, price_history.* 
FROM 
price_history,
product,
product_purchase 
WHERE
product.id = 2
AND price_history.product_id = product.id
AND product_purchase.product_id = product.id

CodePudding user response:

Try Joins and you will have data

SELECT h., p., d.* FROM price_history h

inner join product_purchase p on h.product_id = p.product.id inner join product d on h.product_id = d.id

where p.product.id = 2

  •  Tags:  
  • sql
  • Related