改善查询

| 反正我可以改善此查询吗?
SELECT DISTINCT adb_product_type.strproduct_type AS strproduct_type, adb_product_cat.strproduct_cat AS strproduct_cat, adb_product.strproduct AS strproduct, 

CASE @campaignCount:= 
(SELECT COUNT(DISTINCT adb_campaign_1.campaign_id) 
FROM adb_camp_media AS adb_camp_media_1 
LEFT JOIN adb_campaign AS adb_campaign_1 ON adb_campaign_1.campaign_id = adb_camp_media_1.campaign_id
LEFT JOIN adb_camp_media_prod AS adb_camp_media_prod_1 ON adb_camp_media_prod_1.media_num = adb_camp_media_1.media_num 
WHERE 
(adb_camp_media_1.inttotal_per_store = 1 AND adb_camp_media_1.lngproduct = adb_product.lngproduct OR adb_camp_media_1.inttotal_per_store > 1 AND adb_camp_media_prod_1.lngproduct = adb_product.lngproduct)
AND (
(
adb_campaign_1.start_date
BETWEEN \'2011-01-01\'
AND \'2011-01-31\'
)
OR (
adb_campaign_1.end_date
BETWEEN \'2011-01-01\'
AND \'2011-01-31\'
)
OR (
adb_campaign_1.start_date < \'2011-01-01\'
AND adb_campaign_1.end_date > \'2011-01-31\'
)
) 
) 
WHEN 0 THEN \'No\' ELSE \'Yes\' END AS \'YesNo\', 

CASE @campaignCount WHEN 0 THEN \'\' ELSE @campaignCount END AS \'CampaignCount\', 
CASE @campaignCount WHEN 0 THEN \'\' ELSE 

(SELECT GROUP_CONCAT(DISTINCT adb_media_1.name ORDER BY adb_media_1.name SEPARATOR \', \' ) 
FROM adb_camp_media AS adb_camp_media_1 
LEFT JOIN adb_campaign AS adb_campaign_1 ON adb_campaign_1.campaign_id = adb_camp_media_1.campaign_id 
LEFT JOIN adb_camp_media_prod AS adb_camp_media_prod_1 ON adb_camp_media_prod_1.media_num = adb_camp_media_1.media_num 
LEFT JOIN adb_media AS adb_media_1 ON adb_media_1.media_id = adb_camp_media_1.media_id
WHERE 
(adb_camp_media_1.inttotal_per_store = 1 AND adb_camp_media_1.lngproduct = adb_product.lngproduct OR adb_camp_media_1.inttotal_per_store > 1 AND adb_camp_media_prod_1.lngproduct = adb_product.lngproduct)
AND (
(
adb_campaign_1.start_date
BETWEEN \'2011-01-01\'
AND \'2011-01-31\'
)
OR (
adb_campaign_1.end_date
BETWEEN \'2011-01-01\'
AND \'2011-01-31\'
)
OR (
adb_campaign_1.start_date < \'2011-01-01\'
AND adb_campaign_1.end_date > \'2011-01-31\'
)
) 
) 

END AS mediaName , adb_product.lngproduct

FROM adb_product_type 
LEFT JOIN adb_product_cat ON adb_product_cat.lngproduct_type = adb_product_type.lngproduct_type 
LEFT JOIN adb_product ON adb_product.lngproduct_cat = adb_product_cat.lngproduct_cat 
LEFT JOIN adb_camp_media ON adb_camp_media.lngproduct = adb_product.lngproduct 
LEFT JOIN adb_media ON adb_media.media_id = adb_camp_media.media_id 
LEFT JOIN adb_camp_media_prod ON adb_camp_media_prod.media_num = adb_camp_media.media_num 
LEFT JOIN adb_campaign ON adb_campaign.campaign_id = adb_camp_media.campaign_id 
WHERE 1=1 


ORDER BY YesNo DESC , strproduct_type, strproduct_cat, strproduct 
表结构     -表
adb_camp_media
的表结构 创建表adb_camp_media(   media_num int(11)NOT NULL auto_increment,   campaign_id int(11)NOT NULL默认\'0 \',   media_id int(11)NOT NULL默认\'0 \',   inttotal_per_store int(4)NOT NULL默认\'0 \',   units_per_item int(2)默认为NULL,   lngproduct int(11)NOT NULL默认\'0 \'   rental_cost double(10,2)默认为NULL,   checkers_rental双重默认NULL,   hyper_rental double default NULL,   usave_rental双重默认NULL,   ls_rental double(10,2)默认\'0.00 \',   spar_rental double(10,2)默认\'0.00 \',   背板tinyint(1)NOT NULL,   主键(media_num),   KEY campaign_id(campaign_id,media_id,lngproduct),   密钥lngproduct(lngproduct) )ENGINE = MyISAM DEFAULT CHARSET = latin1; - -表
adb_camp_media_prod
的表结构 创建表adb_camp_media_prod(   media_prod_id int(11)NOT NULL auto_increment,   media_num int(4)NOT NULL,   lngproduct int(4)NOT NULL,   主键(media_prod_id),   密钥media_num(media_num,lngproduct),   密钥lngproduct(lngproduct) )ENGINE = MyISAM DEFAULT CHARSET = latin1; - -表
adb_campaign
的表结构 创建表adb_campaign(   campaign_id int(11)NOT NULL auto_increment,   client_id int(11)NOT NULL默认\'0 \',   campaign_name varchar(50)NOT NULL默认\'\',   说明文字,   position_desc文字,   start_date date NOT NULL默认值\'0000-00-00 \',   end_date date NOT NULL默认值\'0000-00-00 \',   批准的tinyint(1)NOT NULL默认\'0 \',   created_date date NOT NULL默认值\'0000-00-00 \',   Updated_date datetime默认为NULL,   user_id int(11)NOT NULL默认\'0 \',   pinno varchar(50)NOT NULL默认\'\',   jobno varchar(30)NOT NULL默认\'\',   quoteno varchar(30)NOT NULL默认\'\',   sales_ae_id int(11)默认为NULL,   Artwork_specno varchar(30)NOT NULL默认\'\',   Artwork_jobno varchar(30)NOT NULL默认\'\',   代理商tinyint(1)NOT NULL默认为\'0 \',   brand_name varchar(50)NOT NULL默认\'\',   paypercycle tinyint(1)NOT NULL默认\'0 \',   agency_comm双重默认NULL,   变体varchar(100)默认NULL,   pack_size varchar(100)默认为NULL,   折扣double默认NULL,   discount_val double缺省为NULL,   sales_comm double NOT NULL默认为\'0 \',   取消了tinyint(1)默认\'0 \',   保留tinyint(1)默认\'0 \',   campaign_status_id int(4)默认为NULL,   主键(campaign_id),   唯一键jobno(jobno),   KEY开始日期(开始日期),   KEY结束日期(结束日期),   KEY campaign_name(campaign_name),   KEY user_id(用户ID,已取消,保留) )ENGINE = MyISAM DEFAULT CHARSET = latin1; - -表
adb_product
的表结构 创建表adb_product(   lngproduct int(11)NOT NULL auto_increment,   strproduct varchar(100)NOT NULL默认\'\',   lngproduct_cat int(11)NOT NULL默认\'0 \'   主键(lngproduct),   密钥lngproduct_cat(lngproduct_cat) )ENGINE = MyISAM DEFAULT CHARSET = latin1; - -表
adb_product_cat
的表结构 创建表adb_product_cat(   lngproduct_cat int(11)NOT NULL auto_increment,   strproduct_cat varchar(100)NOT NULL默认\'\',   lngproduct_type int(11)NOT NULL默认\'0 \',   主键(lngproduct_cat),   密钥lngproduct_type(lngproduct_type) )ENGINE = MyISAM DEFAULT CHARSET = latin1; - -表
adb_product_type
的表结构 创建表adb_product_type(   lngproduct_type int(11)NOT NULL auto_increment,   strproduct_type varchar(100)NOT NULL默认\'\',   主键(lngproduct_type) )ENGINE = MyISAM DEFAULT CHARSET = latin1;     
已邀请:

要回复问题请先登录注册