我有以下表格:
**products** which has these fields: id,product,price,added_date
**products_to_categories** which has these fields: id,product_id,category_id
**adverts_to_categories** -> id,advert_id,category_id
**adverts** which has these fields: id,advert_name,added_date,location
我无法执行 sql,它将返回给我所有属于类别 14 并且由位于伦敦的广告拥有的产品。所以我有 4 张桌子和 2 个条件 - 来自第 14 类,产品的所有者来自伦敦。我尝试了许多变体来执行 sql,但没有一个结果是正确的。我是否需要使用 Join 以及 which Join - left,right,full?正确的 sql 会是什么样子?提前感谢您的帮助,抱歉让您感到厌烦:)
这是我到目前为止所尝试的:
SELECT p.id, product, price, category_id,
p.added_date, adverts.location, adverts.id
FROM products p,
products_to_categories ptc,
adverts,
adverts_to_categories ac
WHERE ptc.category_id = "14"
AND ptc.product_id=p.id
AND ac.advert_id=adverts.id
AND adverts.location= "London"