mysqlentity-attribute-valuemagento2.2

Get Several Product Attributes as Columns on "catalog_product_entity"


I'm trying to make a table that shows a list of products with various product attributes but I'm completely stuck with the way to do it.

Expected Table looks like below;

entity_id, sku, price, cost
        1, abc,    20,   15
        2, def,    30,   25
        3, ghi,    10,    5

Both product attributes below are stored in the same column "value" on the table "catalog_product_entity_decimal"

Price: attribute_id = 77

Cost: attribute_id = 81

Thus, if I inner join tables, each entity_id has 2 lines for attribute_id 77 and 81

Could you please help me get over this?


Solution

  • You could join the table for attribute two time

    select a.entity_id, a.sku, b.value price, c.value cost
    from your_product_table a
    inner join your_table_attribute b
      on a.entity_id = b.entity_id 
          AND b.attribute_id = 77
    inner join  your_table_attribute c on a.entity_id = c.entity_id
          AND c.attribute_id = 81
    where  a.entity_id = 1