我有以下查询。
select entity_id ,parent_id,name as label , url_key as name from magento_catalog_category_flat_store_1 where entity_id IN(
SELECT distinct T1.parent_id
FROM magento_catalog_category_flat_store_1 T1
INNER JOIN magento_catalog_category_flat_store_1 as T2 ON
T1.entity_id = T2.parent_id
RIGHT JOIN sohyper_region_activity as T3 on
T2.entity_id = T3.activity_id)
由于IN子句,上述查询会降低性能。谁能告诉我如何在这个查询中用JOIN替换IN子句?
感谢。
答案 0 :(得分:9)
一般
SELECT ...
FROM Table1
WHERE col1 IN (SELECT col2 FROM ...)
可以转换为:
SELECT ...
FROM Table1 t1
JOIN (SELECT DISTINCT col2 FROM ...) t2
ON t1.col1 = t2.col2
答案 1 :(得分:0)
试试这个
SELECT Tab1.entity_id ,Tab1.parent_id,Tab1.name As Label , Tab1.url_key As Name
FROM magento_catalog_category_flat_store_1 Tab1 JOIN
(
SELECT distinct T1.parent_id
FROM magento_catalog_category_flat_store_1 T1
INNER JOIN magento_catalog_category_flat_store_1 as T2 ON
T1.entity_id = T2.parent_id
RIGHT JOIN sohyper_region_activity as T3 on
T2.entity_id = T3.activity_id
) Tab2 ON Tab1.entity_id = Tab2.parent_id
答案 2 :(得分:0)
显然没有经过测试!
WITH (
SELECT distinct T1.parent_id
FROM magento_catalog_category_flat_store_1 T1
INNER JOIN magento_catalog_category_flat_store_1 as T2 ON
T1.entity_id = T2.parent_id
RIGHT JOIN sohyper_region_activity as T3 on
T2.entity_id = T3.activity_id)
) T0
select entity_id ,parent_id,name as label , url_key as name from
magento_catalog_category_flat_store_1 T1
INNER JOIN magento_catalog_category_flat_store_1 as T2 ON
T1.entity_id = T2.parent_id
RIGHT JOIN sohyper_region_activity as T3 on
T2.entity_id = T3.activity_id
INNER JOIN T0
ON T0.entity_id = magento_catalog_category_flat_store_1.entity_id