sql - How to select all products satisfying n numbers of attributeid/attributevalue conditions -


i have products table , productattributevalues table

product ------- id   1 name   id   2 name b  productattributevalues ------- id          1 productid   1 attributeid 1 values  z   id          2 productid   1 attributeid 2        1 values  y  id          3 productid   1 attributeid 3        1 values  p 

i need select products where,

((productattributevalues.attrbuteid = x , productattributevalues.value = x)  , (productattributevalues.attrbuteid = x , productattributevalues.value = x)  , ............................................................................  , ............................................................................) 

these conditions dynamic. might 1 or 2 or 3 or on.

so how select products satisfying n numbers of aattributeid/attribute value conditions?

this "set-within-sets" query. think general approach aggregation having caluse:

select productid productattributevalues pav group productid having sum(case when productattributevalues.attrbuteid = x , productattributevalues.value = x                  1 else 0 end) > 0 ,        sum(case when productattributevalues.attrbuteid = y , productattributevalues.value = y                 1 else 0 end) > 0 ,        . . . 

each condition in having clause counts number of attributes each product match condition. overall clause true when conditions have @ least 1 row.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -