sql - Get correct result from mysql query -
i have following tables:
**products** has these fields: id,product,price,added_date **products_to_categories** has these fields: id,product_id,category_id **adverts_to_categories** -> id,advert_id,category_id **adverts** has these fields: id,advert_name,added_date,location i can not execute sql return me products category 14 , owned advert located in london. have 4 tables , 2 conditions - category 14 , owner of product london. tried many variants execute sql none of results correct.. need use join , join - left, right, full? how correct sql like? thank in advance , sorry boring :)
this have tried far:
select p.id, product, price, category_id, p.added_date, adverts.location, adverts.id products p, products_to_categories ptc, adverts, adverts_to_categories ac ptc.category_id = "14" , ptc.product_id=p.id , ac.advert_id=adverts.id , adverts.location= "london"
pretty basic logic
select * products p inner join products_to_categories ptc on p.id = ptc.product_id inner join adverts_to_categories atc on atc.category_id = ptc.category_id inner join adverts ad on ad.id = atc.advert_id ptc.category_id = 14 , ad.location = 'london' you need left or right join if wanted records table didn't exist in other tables.
example, if wanted products if records without category, use left join instead of inner.
Comments
Post a Comment