Please help me with the query:

There is a table of brands in which the brand_id, parent_id and brand_name fields are stored. There are both parents of the brand and their children.

There is a table of goods in which the goods_brandid and brand_sort fields are stored. That is, goods_brandid = brand_id from the brands database.

Actually the question itself is how to get all the brand_sort out of the goods table, where is their parent_id = 3, for example?

I tried to pull it out like this, but it still displays all the values:

SELECT goods.brand_sort, brands.parent_id FROM goods, brands WHERE brands.parent_id = '3' 

Update

Thank you all very much, made a little differently:

 SELECT goods.brand_sort FROM goods LEFT JOIN brands ON (brands.brand_id = goods.goods_brandid) WHERE brands.parent_id = '3' GROUP BY `brand_sort` ;) 
  • How do you join tables? ;-) - Kromster
  • That's it JOIN and I do not understand) - user181354

2 answers 2

answer from the question:


did a little differently:

 SELECT goods.brand_sort FROM goods LEFT JOIN brands ON (brands.brand_id = goods.goods_brandid) WHERE brands.parent_id = '3' GROUP BY goods.brand_sort 

    Add: AND goods.goods_brandid = brands.brand_id

    Update

    Try this.

     SELECT goods.brand_sort, brands.parent_id FROM goods, brands WHERE brands.parent_id = '3' AND goods.goods_brandid = brands.brand_id 
    • Does not work: (Also it copies the value 5-7 times - user181354