使用group by选择mysql更新表

时间:2012-03-22 14:41:43

标签: mysql

我在Windows 7上使用mysql 5.5.9 32位的家伙我的状况非常糟糕需要绝望的帮助 在我的程序中,我有临时表temp01 在我的数据库中,我有代理表,我的存储过程有IN Ico_id int作为参数 随着这个我没有temp01表中的cl列,请记下它。

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';

当我运行存储过程时,它会给出错误:'on cluase'中的未知列tt.t_co_id

1 个答案:

答案 0 :(得分:1)

您的子查询只返回一列(cl)。您将此子查询别名为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列,因为接下来会出现错误。 ;)

相关问题