Find products that have a list of attributes.

Posted by bellesebastien on Stack Overflow See other posts from Stack Overflow or by bellesebastien
Published on 2010-04-02T21:29:49Z Indexed on 2010/04/02 21:33 UTC
Read the original article Hit count: 302

Filed under:
|

I'm stuck trying to solve a problem that's proving to be more difficult than it seems.

Consider there is a table that associates products with attributes, it looks like this:

Products_id | Attribute_id
    21      |      456
    21      |      231
    21      |      26
    22      |      456
    22      |      26
    22      |      116
    23      |      116
    23      |      231

Next, I have a list of attribute_ids which I want to use in order to get the products that have all the attributes in that list.

For example if I search in the table above using this list (456, 26) I should get these product_ids 21 and 22. Another example, if I search for (116, 231) I should get an empty response since there are no products that have both these attributes.

How can I achieve this using one query?

I hope I made my question clear.

Thanks.

© Stack Overflow or respective owner

Related posts about mysql

Related posts about mysql-query