2012-04-05 48 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