2014-01-17 79 views
-1

任何人都可以幫助我把這個基於子查詢的查詢轉換成JOIN的嗎?需要幫助將子查詢轉換爲JOIN的

SELECT DISTINCT brm_products_assets_id, 
(select brm_products_assets_value from brm_products_assets_values where brm_products_assets_id=a.brm_products_assets_id and brm_products_assets_def_id=1) as prod_asset_name, 
(select brm_products_assets_value from brm_products_assets_values where brm_products_assets_id=a.brm_products_assets_id and brm_products_assets_def_id=3) as prod_asset_desc, 
(select brm_products_assets_value from brm_products_assets_values where brm_products_assets_id=a.brm_products_assets_id and brm_products_assets_def_id=2) as prod_asset_type, 
ifnull((select brm_products_assets_value from brm_products_assets_values where brm_products_assets_id=a.brm_products_assets_id and brm_products_assets_def_id=5),'') as file_order 
from brm_products_assets a where last_mnt_option!='d'; 

該查詢當前需要20多秒才能執行。我嘗試了brm_products_assets_value上的索引,但它沒有幫助。

謝謝!

回答

0

你想幹什麼有條件的聚集,而不是單獨的查詢:

SELECT brm_products_assets_id, 
     max(case when brm_products_assets_def_id = 1 then brm_products_assets_value 
      end) as prod_asset_name, 
     max(case when brm_products_assets_def_id = 2 then brm_products_assets_value 
      end) as prod_asset_desc, 
     max(case when brm_products_assets_def_id = 3 then brm_products_assets_value 
      end) as prod_asset_type, 
     max(case when brm_products_assets_def_id = 5 then brm_products_assets_value else '' 
      end) as file_order 
from brm_products_assets a join 
    brm_products_assets_values b 
    on b.brm_products_assets_id = a.brm_products_assets_id 
where last_mnt_option <> 'd' 
group by a.brm_products_assets_id;