使复杂查询更复杂(需要获得完整的术语路径而不是一个级别)

时间:2014-01-24 08:09:58

标签: mysql

我写了一个查询来检索产品变体id和相应的分类术语(词汇目录)。

  1. field_data_field_products 包含field_products_product_id(产品变体)和相应的entity_id(产品,即某些field_products_product_id可以具有相同的entity_id)
  2. taxonomy_term_data 包含tid(术语ID)和名称(术语名称)
  3. field_data_field_catalog 包含field_catalog_tid(术语的ID)和entity_id(产品)
  4. taxonomy_term_hierarchy 包含tid(term id)和parent(parent term tid)
  5. SELECT
      field_data_field_products.field_products_product_id AS p1,
      taxonomy_term_data.name AS p2
    FROM taxonomy_term_data
    INNER JOIN field_data_field_catalog
      ON taxonomy_term_data.tid = field_data_field_catalog.field_catalog_tid
    INNER JOIN field_data_field_products
      ON field_data_field_catalog.entity_id = field_data_field_products.entity_id
    

    我的查询(虽然可能没有优化)检索产品变体ID和相应的分类术语。

    但我有两级术语层次结构。所以,我得到的术语是2级。我想得到它而不是'term_level_1 / term_level_2'组合。我怎样才能做到这一点?是否可以在一个查询中?

1 个答案:

答案 0 :(得分:1)

不确定您想要输出什么,没有表我无法测试它,但这应该为您提供基于代码的东西: -

SELECT
  field_data_field_products.field_products_product_id AS p1,
  taxonomy_term_data.name AS p2
  c.field_products_product_id AS p1,
  a.name AS p2
FROM taxonomy_term_data
INNER JOIN field_data_field_catalog
  ON taxonomy_term_data.tid = field_data_field_catalog.field_catalog_tid
INNER JOIN field_data_field_products
  ON field_data_field_catalog.entity_id = field_data_field_products.entity_id
INNER JOIN taxonomy_term_hierarchy
ON taxonomy_term_data.tid = taxonomy_term_hierarchy.tid
INNER JOIN taxonomy_term_data a
ON a.tid = taxonomy_term_hierarchy.parent
INNER JOIN field_data_field_catalog b
  ON taxonomy_term_data.tid = b.field_catalog_tid
INNER JOIN field_data_field_products c
  ON b.entity_id = c.entity_id