2012-04-05 129 views
2

我有很长的MySQL查询它建立一个报表字段“顶级查询”未知的,这个问题我已经是顶级域“a.id”未知仅向副查询之一,这个查询是从“select with alias”中选择的。我在下面的评论(突出显示)之间设置了违规部分。如何获得子查询中识别的顶级字段?MYSQL:在子查询

请注意,除非高亮选择查询工作正常,但迄今仍未得到“增强”为提高效率,这是第一个快速运行做POC报告。

进一步说明我已经删除了所有公司敏感数据和缩进以帮助我“阅读部分中的查询”,除了所有部分仍然应该链接起来形成功能查询,(再次:禁止违规选择)。 :-)

mysql_query("select distinct 
a.companyname as MEM, 
b.vendorid as VID, 
b.alphacode as ALPHA, 
b.vendorstate as STATE, 
b.exchange as EXC, 
(select count(memberid) from compa_memusergroups where memberid = a.id and software = 'A') as A, 
(select count(memberid) from compa_memusergroups where memberid = a.id and software = 'B') as B, 
(select count(memberid) from compa_memusergroups where memberid = a.id and software = 'C') as C, 
(select count(memberid) from compa_memusergroups where memberid = a.id and software = 'D') as D, 
(select count(memberid) from compa_memusergroups where memberid = a.id and software = 'E') as E, 
(select count(memberid) from compa_memusergroups where memberid = a.id and software = 'F') as F, 
(select count(memberid) from compa_memusergroups where memberid = a.id and software = 'G') as G, 
(select count(memberid) from compa_memusergroups where memberid = a.id and software = 'H') as H, 
(select count(memberid) from compa_memusergroups where memberid = a.id and software = 'I') as I, 
(select count(memberid) from compa_memusergroups where memberid = a.id and software = 'J') as J, 
(select count(memberid) from compa_memusergroups where memberid = a.id and software = 'K') as K, 
(select count(memberid) from compa_memusergroups where memberid = a.id and software = 'L') as L, 
(select count(memberid) from compa_memusergroups where memberid = a.id and software = 'M') as M, 
REPLACE(REPLACE((select count(memberid) from compa_memconndetails where memberid = a.id and type = 'POP'), '0', 'No'), '1', 'Yes') as POP, 
REPLACE(REPLACE(a.editandinfo, '0', ''), '1', 'x') as EDIT, 
REPLACE(REPLACE(a.infoonly, '0', ''), '1', 'x') as INFO, 
(select count(memberid) from compa_oldconnectivity where memberid = a.id and active = 1) as OLD, 
(select count(memberid) from compa_newconnectivity where memberid = a.id and active = 1) as NEW, 
(select count(memberid) from compa_ptconnectivity where memberid = a.id and ptactive = 1) as PTVAL, 
(select count(memberid) from compa_dcconnectivity where memberid = a.id and dcactive = 1) as DCVAL, 
IFNULL((select TRIM(LEADING '0' from substring(sysid,5,2)) from compa_compaconnectivity where memberid = a.id and refid = 'compa1'), 0) as compaLevel1, 
IFNULL((select TRIM(LEADING '0' from substring(sysid,5,2)) from compa_compaconnectivity where memberid = a.id and refid = 'compam'), 0) as compaLevel2, 
IFNULL((select TRIM(LEADING '0' from substring(sysid,5,2)) from compa_compaconnectivity where memberid = a.id and refid = 'compai'), 0) as compaLevel3, 
IFNULL((select TRIM(LEADING '0' from substring(sysid,5,2)) from compa_compaconnectivity where memberid = a.id and refid = 'compan'), 0) as compaLevel4, 
IFNULL((select TRIM(LEADING '0' from substring(sysid,5,2)) from compa_compbconnectivity where memberid = a.id and refid = 'compb1'), 0) as compbLevel1, 
IFNULL((select TRIM(LEADING '0' from substring(sysid,5,2)) from compa_compbconnectivity where memberid = a.id and refid = 'compbm'), 0) as compbLevel2, 
IFNULL((select TRIM(LEADING '0' from substring(sysid,5,2)) from compa_compbconnectivity where memberid = a.id and refid = 'compbi'), 0) as compbLevel3, 
IFNULL((select TRIM(LEADING '0' from substring(sysid,5,2)) from compa_compbconnectivity where memberid = a.id and refid = 'compbn'), 0) as compbLevel4, 
ROUND(
((
(IFNULL((select SUM(compa_memusersoft.bandwold) from compa_memusersoft 
inner join compa_oldconnectivity on compa_memusersoft.acron = compa_oldconnectivity.oldsoft 
where compa_oldconnectivity.memberid = a.id and compa_oldconnectivity.oldsoft not in (select acron from compa_memusersoft where type = 'EXT') and compa_oldconnectivity.active = 1), 0) 
+ IFNULL((select SUM(compa_memusersoft.bandwnew) from compa_memusersoft 
inner join compa_newconnectivity on compa_memusersoft.acron = compa_newconnectivity.newsoft 
where compa_newconnectivity.memberid = a.id and compa_newconnectivity.newsoft not in (select acron from compa_memusersoft where type = 'EXT') and compa_newconnectivity.active = 1), 0) 
+ (IF ((select count(*) from compa_ptconnectivity where memberid = a.id and ptsoft not in (select acron from compa_memusersoft where type = 'EXT') and compa_ptconnectivity.ptactive = 1) > 0, (select count(*) from compa_ptconnectivity where memberid = a.id and ptsoft not in (select acron from compa_memusersoft where type = 'EXT') and compa_ptconnectivity.ptactive = 1) * 0.020, 0)) 
+ (IF ((select count(*) from compa_dcconnectivity where memberid = a.id and dcsoft not in (select acron from compa_memusersoft where type = 'EXT') and compa_dcconnectivity.dcactive = 1) > 0, (select count(*) from compa_dcconnectivity where memberid = a.id and dcsoft not in (select acron from compa_memusersoft where type = 'EXT') and compa_dcconnectivity.dcactive = 1) * 0.352, 0))) 
+ 
(IF ((select count(*) from compa_compaconnectivity 
inner join compa_oldconnectivity on compa_compaconnectivity.memberid = compa_oldconnectivity.memberid 
where compa_compaconnectivity.memberid = a.id and compa_compaconnectivity.refid = 'compa1' and compa_oldconnectivity.oldsoft not in (select acron from compa_memusersoft where type = 'EXT') and compa_oldconnectivity.active = 1) > 0, 0.470 * 2, 
IF((select count(*) from compa_compaconnectivity 
where refid = 'compa1' and memberid = a.id and memberid not in (select memberid from compa_oldconnectivity where memberid = a.id) 
and memberid not in (select memberid from compa_newconnectivity where memberid = a.id) 
and memberid not in (select memberid from compa_ptconnectivity where memberid = a.id) 
and memberid not in (select memberid from compa_dcconnectivity where memberid = a.id)) > 0, 0.470 * 2, 0))) 
+ 
(IF ((select count(*) from compa_compaconnectivity 
inner join compa_newconnectivity on compa_compaconnectivity.memberid = compa_newconnectivity.memberid 
where compa_compaconnectivity.memberid = a.id and compa_compaconnectivity.refid = 'compam' and compa_newconnectivity.newsoft not in (select acron from compa_memusersoft where type = 'EXT') and compa_newconnectivity.active = 1) > 0, 0.893 * 2, 
IF((select count(*) from compa_compaconnectivity 
where refid = 'compam' and memberid = a.id and memberid not in (select memberid from compa_oldconnectivity where memberid = a.id) 
and memberid not in (select memberid from compa_newconnectivity where memberid = a.id) 
and memberid not in (select memberid from compa_ptconnectivity where memberid = a.id) 
and memberid not in (select memberid from compa_dcconnectivity where memberid = a.id)) > 0, 0.893 * 2, 0))) 
+ 
(IF ((select count(*) from compa_compaconnectivity 
inner join compa_oldconnectivity on compa_compaconnectivity.memberid = compa_oldconnectivity.memberid 
where compa_compaconnectivity.memberid = a.id and compa_compaconnectivity.refid = 'compai' and compa_oldconnectivity.oldsoft not in (select acron from compa_memusersoft where type = 'EXT') and compa_oldconnectivity.active = 1) > 0, 0.512 * 2, 
IF((select count(*) from compa_compaconnectivity 
where refid = 'compai' and memberid = a.id and memberid not in (select memberid from compa_oldconnectivity where memberid = a.id) 
and memberid not in (select memberid from compa_newconnectivity where memberid = a.id) 
and memberid not in (select memberid from compa_ptconnectivity where memberid = a.id) 
and memberid not in (select memberid from compa_dcconnectivity where memberid = a.id)) > 0, 0.512 * 2, 0))) 
+ 
(IF ((select count(*) from compa_compaconnectivity 
inner join compa_oldconnectivity on compa_compaconnectivity.memberid = compa_oldconnectivity.memberid 
where compa_compaconnectivity.memberid = a.id and compa_compaconnectivity.refid = 'compan' and compa_oldconnectivity.oldsoft not in (select acron from compa_memusersoft where type = 'EXT') and compa_oldconnectivity.active = 1) > 0, 0.512 * 2, 
IF((select count(*) from compa_compaconnectivity 
where refid = 'compan' and memberid = a.id and memberid not in (select memberid from compa_oldconnectivity where memberid = a.id) 
and memberid not in (select memberid from compa_newconnectivity where memberid = a.id) 
and memberid not in (select memberid from compa_ptconnectivity where memberid = a.id) 
and memberid not in (select memberid from compa_dcconnectivity where memberid = a.id)) > 0, 0.512 * 2, 0))) 
+ 
(IF ((select count(*) from compa_compbconnectivity inner join compa_oldconnectivity on compa_compbconnectivity.memberid = compa_oldconnectivity.memberid 
where compa_compbconnectivity.memberid = a.id and compa_oldconnectivity.oldsoft not in (select acron from compa_memusersoft where type = 'EXT') and compa_oldconnectivity.active = 1) > 0, 0.512 * 2, 
IF((select count(*) from compa_compbconnectivity inner join compa_newconnectivity on compa_compbconnectivity.memberid = compa_newconnectivity.memberid 
where compa_compbconnectivity.memberid = a.id and compa_newconnectivity.newsoft not in (select acron from compa_memusersoft where type = 'EXT') and compa_newconnectivity.active = 1) > 0, 0.512 * 2, 0))) 
+ 
(IF((select count(*) from compa_compbconnectivity where memberid = a.id and memberid not in (select memberid from compa_oldconnectivity where memberid = a.id) 
and memberid not in (select memberid from compa_newconnectivity where memberid = a.id) 
and memberid not in (select memberid from compa_ptconnectivity where memberid = a.id) 
and memberid not in (select memberid from compa_dcconnectivity where memberid = a.id)) > 0, 0.512 * 2, 0)) 
+ 

/*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
IFNULL((select SUM(bandwreq * mplsterm) from (
select compa_memusersoft.acron, compa_memusersoft.bandwreq, compa_memconndetails.mplsterm from compa_memusersoft 
inner join compa_memconndetails on compa_memusersoft.acron = compa_memconndetails.software 
where compa_memusersoft.acron in (select oldsoft from compa_oldconnectivity where memberid = a.id) 
or compa_memusersoft.acron in (select newsoft from compa_newconnectivity where memberid = a.id) 
or compa_memusersoft.acron in (select ptsoft from compa_ptconnectivity where memberid = a.id) 
or compa_memusersoft.acron in (select dcsoft from compa_dcconnectivity where memberid = a.id) 
and compa_memconndetails.type = 'EXT' and compa_memconndetails.memberid = a.id group by compa_memusersoft.acron) tblval), 0) 
*/------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

) 
/(70/100) 
), 3) as REQBANDW, 
(IFNULL((select pribandw1 from compa_currmembandw where memberid = a.id), 0) + IFNULL((select pribandw2 from compa_currmembandw where memberid = a.id), 0) + 
IFNULL((select secbandw1 from compa_currmembandw where memberid = a.id), 0) + IFNULL((select secbandw2 from compa_currmembandw where memberid = a.id), 0) + 
IFNULL((select backbandw1 from compa_currmembandw where memberid = a.id), 0) + IFNULL((select backbandw2 from compa_currmembandw where memberid = a.id), 0)) as CURRBANDW, 
a.env 
from 
compa_member a 
inner join compa_memberconnectivity b on a.id = b.memberid 
order by a.env desc, a.companyname"); 
+0

在有问题的部分子查询没有多大意义的我。你被'acron'分组也检索等栏目,其中一个属于连接表了。应该认识到,如果连接产生每ACRON多行,你是拉不参加分组非聚集值,这些值将是随机的。而且,最重要的所有,外子查询执行计算('SUM(compa_memusersoft.bandwreq * compa_memconndetails.mplsterm)')上的那些潜在的随机值。这是设计吗? – 2012-04-05 14:01:51

回答

1

我建议修改违规的部分是这样的:

IFNULL(
    (
    /*select SUM(bandwreq * mplsterm) 
    from (*/ 
     select /*compa_memusersoft.acron,*/ SUM(compa_memusersoft.bandwreq * compa_memconndetails.mplsterm) 
     from compa_memusersoft 
     inner join compa_memconndetails on compa_memusersoft.acron = compa_memconndetails.software 
     where compa_memusersoft.acron in (select oldsoft from compa_oldconnectivity where memberid = a.id) 
     or compa_memusersoft.acron in (select newsoft from compa_newconnectivity where memberid = a.id) 
     or compa_memusersoft.acron in (select ptsoft from compa_ptconnectivity where memberid = a.id) 
     or compa_memusersoft.acron in (select dcsoft from compa_dcconnectivity where memberid = a.id) 
     and compa_memconndetails.type = 'EXT' 
     and compa_memconndetails.memberid = a.id 
     group by compa_memusersoft.acron 
    /*) tblval*/ 
), 
    0 
) 
+0

非常感谢舍甫琴科,你的建议,工作得很好,除了它返回多行,我再“分组”所有“或选择”到一个不同的选择,并更改它完美地工作。感谢您的帮助,非常感谢。 (现在坐下来优化整个事情!!!) – vigilantis 2012-04-06 15:53:08