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

时间:2016-04-12 05:29:33

标签: mysql

我很难围绕product_count列的工作方式。它似乎以某种方式引用了别名seee,它们都指向catalog_category_entity。具体来说,

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

1 个答案:

答案 0 :(得分:1)

这是一个相关查询的示例,此处查询所有者正在使用OR子句检查子查询外部的条件。

在下面的子查询中,有distinct count production_idcatalog_category_entity,它们应该存在于两个表中:catalog_category_productentitiy_id(即使在这里不需要左连接,内连接可以更好地工作,因为你从右边的表中得到计数)条件是catalog_category_entity应该存在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

如果要求明确,您可以简化查询,因为您使用左连接加入表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 4次,而您只能使用以下一次:

   public void checkduplicate()
    {
         char chnew =' ';
         char[] ch ={ 'a', 'c', 'd', 'b' , 'a', 'b'};

         for(int i =0 ; i<ch.length;i++)
         {                        
             for(int j =i+1 ;j<ch.length;j++)
             {

                 if(ch [j]== ch[i] )
                 {
                     ch[j] = ' ';
                 }
              if(ch[j] <ch[i] && ch[j] != ' ' )
             {
                 chnew = ch[i];            
                 ch[i] = ch[j];
                 ch[j] = chnew;                                
             }

             }

         }
         for(int k=0;k<ch.length;k++)
         {
             System.out.println(ch[k]);
         }

     }
相关问题