2

我已经成功地尝试了这个查询,但有一个限制。以下查询无限制地无限运行:

SELECT o.product_sku
FROM   order_table o
WHERE  EXISTS (SELECT 1
           FROM   (SELECT DISTINCT u.type_id,
                                   u.charge_type,
                                   u.billed_weight
                   FROM   ups_table u
                   WHERE  charge_type = 'order_shipping_table'
                          AND NOT billed_weight = '0'
                   ) dtm
           WHERE  o.order_id = dtm.type_id)
GROUP  BY product_sku
HAVING Count(product_sku) > 1
4

1 回答 1

0

首先,您不需要这些带有 DISTINCT 的子查询等等:

SELECT o.product_sku
FROM   order_table o
WHERE  EXISTS (SELECT 1
                   FROM   ups_table u
                   WHERE  charge_type = 'order_shipping_table'
                          AND NOT billed_weight = '0'
                          AND type_id=o.order_id
               )
GROUP  BY product_sku
HAVING Count(product_sku) > 1

我不知道你的表结构(你想在 HAVING 中计算什么?)但你可以尝试将 EXISTS 更改为 JOIN:

SELECT o.product_sku
FROM   order_table o
JOIN ups_table u on (u.type_id=o.order_id)
                    AND (u.charge_type = 'order_shipping_table')
                    AND NOT (billed_weight = '0')
GROUP  BY product_sku
HAVING Count(DISTINCT o.order_id) > 1

你需要索引o.order_id, o.product_sku, u.type_id,u.charge_type, u.billed_weight

于 2013-11-14T12:33:32.763 回答