2016-04-12 17 views
0

Kafamı product_count sütununun nasıl çalıştığına dair etrafımı saran zor bir zaman geçiriyorum. Her nasılsa, her ikisi de catalog_category_entity işaret eden see ve e takma adlarıyla kendini gösterir gibi görünüyor. Özellikle,MySQL: Kendinden Referanslı Oluşturulan Sütun (?) SELECT Sorgu

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

ve e masanın catalog_category_entity rumuzudur. Bu ne yapıyor?

İşte tüm sorgu var:

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

bu belki de Gördüğüm en kendine ait sorgu. Bununla ne elde etmeye çalışıyorsun? Bahse girerim oraya varmanın çok daha iyi yolları var ...... –

+0

Ürün sayımlarını, diğer birçok şeyin yanı sıra, “24” ve “533” kategorilerinde döndürürüm. Bunu ben yazmadım ve sadece 'product_count' sütununun WHERE' deyiminin nasıl çalıştığını merak ediyorum. – musicliftsme

cevap

1

Bu bağıntılı sorgusuna bir örnektir, burada sorgu sahibinin bir OR maddesi ile alt sorgunun dışarıdan bir koşul kontrol ediyor. Orada sol hiçbir gereklilik katılmak olduğunu ve doğru saymaya olsun bir iç daha iyi çalışabilir katılmak catalog_category_entity ve catalog_category_product (hatta burada: Aşağıdaki alt sorguda

her iki tabloda olması gereken production_id bir distinctcount var entitiy_id ana tablo OR ana alt tablo olarak mevcut olması koşuluyla alt taraf sorgulama alanı alanı, ana alan tablosunun sol tarafındaki ana alanla eşleşmesi gerektiği anlamına gelir. aynı olmak

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, '/%')) 
sen masayı catalog_category_entity_int sol ile 4 kez katılmak katılmadan gibi gereklilik açıktır eğer aşağıdaki gibi yalnızca bir defa kullanabilirsiniz ederken, Sorgunuzla kolaylaştırabilirsiniz

:

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

"see" ve "e" arasındaki karşılaştırmadan başka her şeyi alıyorum çünkü her ikisi de 'catalog_category_entity' ana tablosu için takma adlardır. Peki "see.entity_id = e.entity_id" nedir? – musicliftsme

+0

İlişkili alt sorguları takip ettim ve okudum ve şimdi anlıyorum. Alt sorgunun "WHERE" (örn. 'See.entity_id = e.entity_id') dış sorgudan (yani' SELECT' dışından e.entity_id's) döndürülen verileri kullanıyor. – musicliftsme