Skip to content

Instantly share code, notes, and snippets.

@dfelton
Last active November 6, 2020 13:54
Show Gist options
  • Save dfelton/06bf7331bfea5284c8e7934bc4d4d8b0 to your computer and use it in GitHub Desktop.
Save dfelton/06bf7331bfea5284c8e7934bc4d4d8b0 to your computer and use it in GitHub Desktop.
Magento 2 Queries

Add in conditions at the bottom for filtering.

SELECT
   `entity`.`entity_type_code`,
   `attr`.`attribute_code`,
   `store`.`code` AS `store_code`,
   `store`.`store_id`,
   `option`.`option_id`,
   `option`.`sort_order`,
   `label`.`value` AS `option_label`,
   `swatch`.`value` AS `option_swatch`
FROM `eav_attribute_option` AS `option`
INNER JOIN `eav_attribute_option_value` AS `label`
  ON `option`.`option_id` = `label`.`option_id`
INNER JOIN `store`
  ON `label`.`store_id` = `store`.`store_id`
LEFT JOIN `eav_attribute_option_swatch` AS `swatch`
  ON `label`.`option_id` = `swatch`.`option_id` 
  AND `swatch`.`store_id` = `store`.`store_id`
INNER JOIN `eav_attribute` AS `attr`
  ON `option`.`attribute_id` = `attr`.`attribute_id` 
INNER JOIN `eav_entity_type` AS `entity`
  ON `attr`.`entity_type_id` = `entity`.`entity_type_id`


ORDER BY
  `entity`.`entity_type_code` ASC,
  `attr`.`attribute_code` ASC,
  `store`.`store_id` ASC,
  `option`.`sort_order` ASC
;

Optional filtering:

WHERE `entity`.`entity_type_code` = 'REPLACE_WITH_ENTITY_TYPE_CODE'
AND `store`.`code` IS NOT NULL
AND `attr`.`attribute_code` = 'REPLACE_WITH_ATTRIBUTE_CODE'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment