Create an SQL script to find a simple product name with its configurable product SKU In Magento 2 [Magento 2]
Anyone with a better solution than my one are appreciated.
Please share your answer.
Query as follows:
select
nametable.value as product_name,
parent.type_id as parent_product_type, parent.sku as parent_sk,
child.type_id as child_product_type, child.sku child_sku
from catalog_product_entity as parent
join catalog_product_relation as link
on link.parent_id = parent.entity_id
join catalog_product_entity as child
on child.entity_id = link.child_id
join catalog_product_entity_varchar as nametable ON nametable.row_id = parent.entity_id
AND nametable.store_id = 0
AND nametable.attribute_id = (SELECT attribute_id
FROM eav_attribute
WHERE attribute_code = 'name'
AND entity_type_id =
(SELECT entity_type_id
FROM eav_entity_type
WHERE entity_type_code = 'catalog_product'))
where parent.sku = 'product-sku'
If my answer helped you, you can accept the answer so it can help others also.
Optimized for Magento Commerce 2.3.3 ( formerly Enterprise Edition) - The parent uses row_id instead of entity_id. Also, removed store_id as it's unnecessary if you have one store and potentially wrong if you have multi store.
SELECT
nametable.value child_name,
parent.type_id parent_product_type,
parent.sku parent_sku,
child.type_id child_product_type,
child.sku child_sku
FROM
catalog_product_entity parent
JOIN catalog_product_relation link
ON link.parent_id = parent.row_id
JOIN catalog_product_entity child
ON child.entity_id = link.child_id
JOIN catalog_product_entity_varchar nametable
ON nametable.row_id = parent.row_id
AND nametable.attribute_id =
(SELECT
attribute_id
FROM
eav_attribute
WHERE attribute_code = 'name'
AND entity_type_id =
(SELECT
entity_type_id
FROM
eav_entity_type
WHERE entity_type_code = 'catalog_product'))
WHERE parent.sku = 'YOUR-SKU';