简体   繁体   English

MySQL:从自引用(?)SELECT查询生成的列

[英]MySQL: Column Generated From a Self-Referencing (?) SELECT Query

I'm having a hard time wrapping my head around how the column product_count works. 我很难理解product_count列的工作方式。 It seems to be somehow referencing itself with aliases see and e , which are both pointing to catalog_category_entity . 似乎以某种方式使用别名seee来引用自身,这两个别名都指向catalog_category_entity Specifically, 特别,

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

Both see and e are aliases for table catalog_category_entity . seee都是表catalog_category_entity别名。 What is this doing? 这是在做什么

Here's the entire query: 这是整个查询:

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

This is an example of a correlated query, here the query owner is checking a condition from outside of the sub-query with an OR clause. 这是相关查询的示例,此处查询所有者正在使用OR子句从子查询外部检查条件。

In the below sub-query there is a distinct count of production_id which should exist in both tables: catalog_category_entity and catalog_category_product (even here there is no requirement of left join and an inner join may work better as you get count from the right side table) with condition that either entitiy_id should exist in catalog_category_entity as main table OR sub-query path field should match with the main table left part of path field means the main table may contain an extra string in right side but the left part should be same. 在下面的子查询中,两个表中都应存在一个distinctproduction_id countcatalog_category_entitycatalog_category_product (即使在这里也不需要左连接,并且当您从右侧表中获得计数时,内部连接可能会更好地工作)条件是entitiy_id应该作为主表存在于catalog_category_entityOR子查询路径字段应与主表相匹配,即路径字段的左侧部分意味着主表的右侧可能包含一个额外的字符串,但左侧部分应该相同。

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, '/%'))

You can simplify your query if the requirement is clear as you are joining table catalog_category_entity_int 4 times with left join, while you can use as below only a single time: 如果您的要求很明确,则可以简化查询,因为您通过左catalog_category_entity_int将表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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM