Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8532659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:45:56+00:00 2026-06-11T09:45:56+00:00

I have a product catalog with a attribute system. I want to create a

  • 0

I have a product catalog with a attribute system.

I want to create a system that will allow client to browse products by attribute (like on Magento). So, I am trying to programatically retrieve a list of products (by ID return) that have a attribute with a specific value (or many attributes with many specific values).

It works well, but my SQL query is horrible!

For exemple: if I want to retrieve a list of products that have these 3 attributes (and values), here’s my current SQL query:

  • 1st filter: id_attribut = 3 AND value = ‘Lorem ipsum’
  • 2nd filter: id_attribut = 4 AND value = ‘Test2’
  • 3rd filter: id_attribut = 2 AND value = ‘Bar2’

    SELECT products.id
    FROM products
    JOIN products_attributs ON products_attributs.id_product = products.id
    WHERE products_attributs.value = 'Lorem ipsum'
    AND products_attributs.id_attribut = 3
    AND products_attributs.id_product            ==> return ID: 25, 27
    IN (
        SELECT id_product
        FROM products_attributs
        WHERE id_attribut = 4
        AND value = 'Test2'                  ==> return ID: 27
        AND id_product IN (
            SELECT id_product
            FROM products_attributs
            WHERE id_attribut = 2
            AND value = 'Bar2'           ==> return ID: 27
        )
    )

Result: return ID 27 (this is the only product that corresponds to the three attributes selected by the customer).

It works well but it’s not elegant and not very optimised (imagine if I have more than 10 filters !).

The products_attributs table:

-------------------------------------------------
| id | id_product | id_attribut | value         |
-------------------------------------------------
| 1  | 25         | 1           | Foo           |
| 2  | 25         | 2           | Bar           |
| 3  | 25         | 3           | Lorem ipsum   |
| 4  | 25         | 4           | Test          |
| 5  | 27         | 1           | Foo2          |
| 6  | 27         | 2           | Bar2          |
| 7  | 27         | 3           | Lorem ipsum   |
| 8  | 27         | 4           | Test2         |
| 9  | 28         | 1           | ... etc       |
-------------------------------------------------

How can I improve that?

Thank a lot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:45:57+00:00Added an answer on June 11, 2026 at 9:45 am

    The only way to have “nice” SQL is to make columns for all of your attributes. That’s much harder to maintain, so I wouldn’t recommend it.

    Given than, you can join back to your attributes table multiple times, like this:

    SELECT p.id
    FROM products p
    LEFT OUTER JOIN products_attributs pa1 ON pa1.id_product = p.id and pa1.value = 'Lorem ipsum' AND pa1.id_attribut = 3
    LEFT OUTER JOIN products_attributs pa2 ON pa2.id_product = p.id and pa2.value = 'Test2' AND pa1.id_attribut = 4
    LEFT OUTER JOIN products_attributs pa3 ON pa3.id_product = p.id and pa3.value = 'Bar2' AND pa1.id_attribut = 2
    

    Good luck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Summary I want to create a product attribute that is not saved to products,
Let's say I have a collection like: $products = Mage::getModel('catalog/product') ->getCollection() ... ->load(); How
I have an attribute defined for a product that will contain a comma separated
I have to develop a online product catalog which will eventually developed into a
i have created a list_home.phtml that i call from {{block type=catalog/product_list category_id=6 template=catalog/product/list_home.phtml}} The
I have a product attribute that I have set the scope to website (so
I want to make a custom collection query to load the products that have
I have to create a simple Magento 1.6.x import agent that suppose to create/update
I have a product catalog. Each category consists of different number (in deep) of
I have a product catalog using apache, php and mysql. I need to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.