Magento 2: How to get product name by sql query using product ID
Magento uses an EAV table structure for products. Names are stored in catalog_product_entity_varchar
. If you have the product ID X, you can query this table directly:
SELECT entity_id, value, store_id FROM catalog_product_entity_varchar
WHERE entity_id = X AND attribute_id = (
SELECT attribute_id FROM eav_attribute
WHERE entity_type_id=4 AND attribute_code='name'
)
The subquery determines the attribute id for "name", entity_type_id 4 is always the type ID for products.
Note that in a multistore setup you might get multiple values per product. Pay attention to the "store_id" column. "0" stands for the default value.
If your product's id is 1234:
select value from catalog_product_entity_varchar left join eav_attribute on
eav_attribute.attribute_id = catalog_product_entity_varchar.attribute_id
where
eav_attribute.attribute_code='name' and
catalog_product_entity_varchar.entity_id=1234