Mysql: использовать значение столбца таблицы соединения в предложении where - PullRequest
0 голосов
/ 24 апреля 2018

catalog_product_entity стол

enter image description here

catalog_product_relation стол

enter image description here

catalog_product_entity_varchar таблица

enter image description here

Я хочу экспортировать sku и url из приведенных выше таблиц:

  • sku в таблице catalog_product_entity.
  • url в таблице catalog_product_relation.

Я попытался объединить эти три таблицы и выбрать значения url, если таблицы catalog_product_entity entity_id совпадает со столбцом child_id в catalog_product_relation, а затем использовать parent_id в выражении where, в других случаях использовать entity_id;

select  cpe.sku
,       value 
from    catalog_product_entity as cpe
left join 
        catalog_product_entity_varchar as cpev
on      cpe.entity_id = cpev.entity_id
where   cpev.attribute_id = 119 and 
        type_id = "simple" and 
        cpev.store_id=0 and 
        cpev.entity_id = 
        (
        select  parent_id
        from    catalog_product_relation 
        where   child_id = cpe.entity_id
        )

Вышеупомянутый запрос не является правильным запросом, который я загрузил здесь для понимания

Редактировать:

catalog_product_entity_varchar заголовок таблиц

enter image description here

Ответы [ 2 ]

0 голосов
/ 24 апреля 2018

Вы можете join значение дважды, один раз напрямую и один раз через промежуточную таблицу parent-child. Используйте coalesce для возврата к дочернему значению, когда родительское значение не найдено:

select  cpe.sku
,       coalesce(parent_cpev.value, child_cpev.value)
from    catalog_product_entity as cpe
left join 
        catalog_product_entity_varchar as child_cpev
on      child_cpev.entity_id = cpe.entity_id
        and child_cpev.attribute_id = 119
        and child_cpev.store_id = 0
left join
        catalog_product_relation as cpr
on      cpe.entity_id = cpr.child_id
left join 
        catalog_product_entity_varchar as parent_cpev
on      parent_cpev.entity_id = cpr.parent_id
        and parent_cpev.attribute_id = 119
        and parent_cpev.store_id = 0
where   cpe.type_id = 'simple'
0 голосов
/ 24 апреля 2018

В столбце type_id отсутствует (отсутствует) префикс псевдонима таблицы, указанный в вашем запросе.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...