D
D
DIvan4ik2016-08-30 01:15:43
MySQL
DIvan4ik, 2016-08-30 01:15:43

How to form a correct query in a linked table?

Let's say there are 3 tables: Products, Attributes, Attribute_values ​​(linked to the other two by attribute/product_id).
There are 2 products - Computer chair and Armchair
There are 3 Attributes - Backrest, Armrests, Adjustable height.
And the question is: How to pull out products that have all 3 attributes available (= 1) through an adjacent table? (only a computer chair should turn out)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vyacheslav Onufriyuk, 2016-08-30
@fynivx

Like this, for example (this is T-SQL):

select P.*
  from Products as P
  join Attribute_values as AV
    on AV.ProductID = P.ID
   and AV.Value = 1
   and AV.AttributeID in (attr1, attr2, attr3)
 group by P.*
having count(AV.ID) = 3

Well, of course the ProductID/AttributeID combination in Attribute_values ​​must be unique.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question