2012-03-22 58 views
0

上午使用MySQL 5.5.9在Windows 7 32位傢伙是真的很糟糕的情況需要絕望的幫助 在我的程序中我有臨時表temp01 和在我的數據庫我有代理表和我的存儲過程有IN Ico_id int作爲參數 與此一起,我沒有在temp01表cl列請記下它。Mysql更新表與選擇使用組

update agent a 
join ( 
    select sum(ifnull(t_dr_amt,0)) - sum(ifnull(t_cr_amt,0)) as cl 
    from temp01 
    group by t_ac_id 
) as tt 
    on a.co_id = tt.t_co_id 
    and a.agent_id = tt.t_ac_id 
SET a.cl = tt.cl 
where a.co_id = 1 
AND lower(a.io) = 'y'; 

當我運行存儲過程把它給我的錯誤:在「上cluase」

回答

1

你的子查詢只返回一列(cl)未知列tt.t_co_id。您將此子查詢別名爲tt,因此沒有tt.t_co_id。如果這是在temp01列,你可以把它改成這樣:

select 
    sum(ifnull(t_dr_amt,0)) - sum(ifnull(t_cr_amt,0)) as cl, 
    t_co_id, 
    t_ac_id 
from temp01 
group by t_ac_id 

您選擇t_co_id列以及這種方式。我還添加了t_ac_id列,因爲您接下來會看到該錯誤。 ;)