我得到#1066 - 不是唯一的表/别名:' pf'错误在这里

时间:2016-05-10 06:05:17

标签: mysql unique

SELECT COUNT(DISTINCT `product_id`) AS `total`
FROM(
    SELECT DISTINCT `p`.`product_id`
    FROM oc_category_path cp
    LEFT JOIN oc_product_to_category p2c ON (cp.category_id = p2c.category_id)
    LEFT JOIN oc_product_filter pf ON (p2c.product_id = pf.product_id)
    LEFT JOIN oc_product p ON (pf.product_id = p.product_id)
    LEFT JOIN oc_product_description pd ON (p.product_id = pd.product_id)
    LEFT JOIN oc_product_to_store p2s ON (p.product_id = p2s.product_id)  
    INNER JOIN `oc_product_filter` AS `pf` ON `p2c`.`product_id` = `pf`.`product_id`
    WHERE IF( `p`.`quantity` > 0, 7, `p`.`stock_status_id` ) IN(7)
        AND  `p`.`date_available` <= NOW()
        AND `p`.`status` = '1'
        AND `cp`.`path_id` = '452'
        AND `pf`.`filter_id` IN(241)
        AND  pd.language_id = '1'
        AND p.status = '1'
        AND p.date_available <= NOW()
        AND p2s.store_id = '0'
        AND cp.path_id = '452'
        AND pf.filter_id IN (241)
) AS `tmp` 

1 个答案:

答案 0 :(得分:1)

这一行...

LEFT JOIN oc_product_filter pf ON (p2c.product_id = pf.product_id)

...与此行共享一个表别名...

INNER JOIN `oc_product_filter` AS `pf` ON `p2c`.`product_id` = `pf`.`product_id`

那不可能发生。所以给第二个别名别名......

SELECT COUNT(DISTINCT product_id) total
 FROM
    ( SELECT DISTINCT p.product_id
                 FROM oc_category_path cp
                 LEFT 
                 JOIN oc_product_to_category p2c 
                   ON p2c.category_id = cp.category_id
                 JOIN oc_product_filter pf 
                   ON pf.product_id = p2c.product_id
                 JOIN oc_product p 
                   ON p.product_id = pf.product_id
                 JOIN oc_product_description pd 
                   ON pd.product_id = p.product_id
                 JOIN oc_product_to_store p2s 
                   ON p2s.product_id = p.product_id
                 JOIN oc_product_filter pf2              -- <-- changed
                   ON pf.product_id = p2c.product_id
                WHERE IF(p.quantity > 0, 7, p.stock_status_id ) IN(7)
                  AND p.date_available <= NOW()
                  AND p.status = 1
                  AND cp.path_id = 452
                  AND pf.filter_id IN(241)
                  AND pd.language_id = 1
                  AND p.status = 1
                  AND p.date_available <= NOW()
                  AND p2s.store_id = 0
                  AND cp.path_id = 452
                  AND pf2.filter_id IN(241)              -- <-- changed
    ) tmp;