MYSQL:子查询中未知的“顶级查询”中的字段

时间:2012-04-05 11:15:17

标签: mysql

我有一个很长的mysql查询来构建报告,我遇到的问题是顶级字段“a.id”仅对其中一个子查询是未知的,此查询是从“选择别名”中选择的”。我在下面的评论(突出显示)之间设置了违规部分。如何在子查询中识别顶级字段?

请注意,除非突出显示,否则查询工作正常并且尚未“提高”效率,这是第一次快速执行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");

1 个答案:

答案 0 :(得分: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
)