2012-04-05 3 views
2

私はレポートを作成する長いmysqlクエリを持っていますが、問題はトップレベルフィールド "a.id"がサブクエリの1つにしか不明であるということですこのクエリは、「別名で選択」からの選択です。私は以下のコメントの間に問題のセクションを設定しました(強調表示)。サブクエリでトップレベルのフィールドを認識させるにはどうすればよいですか?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'でグループ化するだけでなく、結合されたテーブルに属する他の列も取得します。結合によってアクロンごとに複数の行が生成され、グループ化に関与しない非集計値を引き出している場合、それらの値はランダムになります。そして、それをすべて上にするために、外部サブクエリはそれらの潜在的にランダムな値に対して計算( 'SUM(compa_memusersoft.bandwreq * compa_memconndetails.mplsterm)')を実行します。これは設計によるものですか? –

答えて

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

多くのおかげでAndriy、あなたの提案はうまくいきました。ただし、複数の行が返された場合を除き、私はすべての "グループ化"を選択して1つの選択肢にしました。助けてくれてありがとう、非常に感謝します。 (今は座って全体を最適化する!!!) – vigilantis