2016-04-12 18 views
0

product_countがどのように機能するかについて頭を悩ましています。何らかの形で別名seeeが両方ともcatalog_category_entityを指しているようです。具体的には、MySQL:自己参照(?)SELECTクエリから生成される列

WHERE (see.entity_id = e.entity_id) OR (see.path LIKE CONCAT(e.path, '/%')) 

両方seeeテーブルcatalog_category_entityの別名です。これは何ですか?ここで

は、クエリ全体です:

SELECT `e`.*, `d_name`.`value` AS `name`, IF(s_name.value_id > 0, s_name.value, d_name.value) AS `name`, `d_is_active`.`value` AS `is_active`, IF(s_is_active.value_id > 0, s_is_active.value, d_is_active.value) AS `is_active`, `d_is_anchor`.`value` AS `is_anchor`, IF(s_is_anchor.value_id > 0, s_is_anchor.value, d_is_anchor.value) AS `is_anchor`, 
    (
     SELECT COUNT(DISTINCT scp.product_id) 
     FROM `catalog_category_entity` AS `see` 
      LEFT JOIN `catalog_category_product` AS `scp` 
       ON see.entity_id=scp.category_id 
     WHERE (see.entity_id = e.entity_id) OR (see.path LIKE CONCAT(e.path, '/%')) 
    ) AS `product_count`, 
    (
     SELECT COUNT(cp.product_id) 
     FROM `catalog_category_product` AS `cp` 
     WHERE (cp.category_id = e.entity_id) 
    ) AS `self_product_count` 
FROM `catalog_category_entity` AS `e` 
    LEFT JOIN `catalog_category_entity_varchar` AS `d_name` ON d_name.entity_id=e.entity_id AND d_name.attribute_id=41 AND d_name.entity_type_id=e.entity_type_id AND d_name.store_id=0 
    LEFT JOIN `catalog_category_entity_varchar` AS `s_name` ON s_name.entity_id=e.entity_id AND s_name.attribute_id=41 AND s_name.entity_type_id=e.entity_type_id AND s_name.store_id=0 
    LEFT JOIN `catalog_category_entity_int` AS `d_is_active` ON d_is_active.entity_id=e.entity_id AND d_is_active.attribute_id=42 AND d_is_active.entity_type_id=e.entity_type_id AND d_is_active.store_id=0 
    LEFT JOIN `catalog_category_entity_int` AS `s_is_active` ON s_is_active.entity_id=e.entity_id AND s_is_active.attribute_id=42 AND s_is_active.entity_type_id=e.entity_type_id AND s_is_active.store_id=0 
    LEFT JOIN `catalog_category_entity_int` AS `d_is_anchor` ON d_is_anchor.entity_id=e.entity_id AND d_is_anchor.attribute_id=51 AND d_is_anchor.entity_type_id=e.entity_type_id AND d_is_anchor.store_id=0 
    LEFT JOIN `catalog_category_entity_int` AS `s_is_anchor` ON s_is_anchor.entity_id=e.entity_id AND s_is_anchor.attribute_id=51 AND s_is_anchor.entity_type_id=e.entity_type_id AND s_is_anchor.store_id=0 
WHERE (`e`.`entity_type_id` = '3') AND (e.entity_id IN('24', '533')) ORDER BY LENGTH(e.path) ASC 
+0

これは多分あります私が見たほとんどの自己結合クエリ。あなたはそれで何を達成しようとしていますか?私はそこにあなたを連れて行くもっと良い方法があると確信しています...... –

+0

私は、カテゴリ '24'と '533'の製品の数を他の多くのものか​​ら返します。私はこれを書いておらず、 'product_count'カラムの' WHERE'文がどのように働くのか不思議です。 – musicliftsme

答えて

1

これは相関クエリの例であり、ここでは、クエリの所有者はOR句でサブクエリの外から条件をチェックしています。 catalog_category_entitycatalog_category_product(ここでも左のない要件が加わるありません、あなたがからカウントを取るにつれて、内部結合をよりよく働くことがあります。サブクエリの下に両方のテーブルに存在している必要がありますproduction_iddistinctcountがあるで

右側のテーブル)メインテーブルとしてentitiy_idが存在する必要がありますORサブクエリのパスフィールドがメインテーブルの左側のパスフィールドと一致している必要があります。メインテーブルには右側に余分な文字列が含まれていますが、同じです。

SELECT COUNT(DISTINCT scp.product_id) 
    FROM `catalog_category_entity` AS `see` 
    LEFT JOIN `catalog_category_product` AS `scp` 
     ON see.entity_id=scp.category_id 
    WHERE (see.entity_id = e.entity_id) 
    OR (see.path LIKE CONCAT(e.path, '/%')) 
あなたがテーブル catalog_category_entity_intに左で4回ジョイン参加しているような要件が明確である場合は、以下のように一度だけ使用することができますしながら、あなたは、あなたのクエリを簡素化することができます

は:

LEFT JOIN catalog_category_entity_int` AS `d_is_anchor` 
ON d_is_anchor.entity_id=e.entity_id 
    AND d_is_anchor.attribute_id IN (42,51) 
    AND d_is_anchor.entity_type_id=e.entity_type_id 
    AND d_is_anchor.store_id=0 
+0

メインテーブル 'catalog_category_entity'のエイリアスであるため、' see'と 'e'の比較以外のものはすべて取得しました。だから 'see.entity_id = e.entity_id'とは何ですか? – musicliftsme

+0

私はフォローアップして相関サブクエリについて読んだので、今理解しています。サブクエリの「WHERE」(例えば、 'see.entity_id = e.entity_id')は、外部クエリから返されたデータを使用しています(つまり、外側の' SELECT'からの 'e.entity_id')。 – musicliftsme