0

我尝试了以下 MySQL 更新,但它只设置了第一个字段c.avg_price_dil而不是第二个字段c.avg_dilution

奇怪的是,当我将更新分成如下所示的两部分时,两者都可以同时更新两个字段。是否存在影响此调用的基于组的函数?

更新了两个字段- 仅c.avg_price_dil更新。

update corp_act as c
inner join (
  select 
      m.gvkey, m.gvkey_iid, m.date_base, avg(m.price_dil) as avg_price, 
      avg(m.dilution) as avg_dilution
  from master as m
  inner join corp_act as c
  on c.gvkey = m.gvkey AND
      m.date_base between c.date_base AND 
                          LAST_DAY(c.date_base + INTERVAL 11 MONTH)
  where c.actioncd not in ('N/C','REA','SPN') AND 
        c.actioncd IS NOT NULL
  group by m.gvkey_iid, c.actioncd) as x
on 
   x.gvkey = c.gvkey AND 
   x.date_base = c.date_base
set 
    c.avg_price_dil = if(x.avg_price is null, 0, x.avg_price) AND
    c.avg_dilution = if(x.avg_dilution is null, 0, x.avg_dilution)

当分成两个更新时,两者都有效:

稀释更新: - 工作正常

update corp_act as c
inner join (
  select 
       m.gvkey, m.gvkey_iid, m.date_base, avg(m.dilution) as avg_dilution
  from master as m
  inner join corp_act as c
  on c.gvkey = m.gvkey AND
     m.date_base between c.date_base AND 
                         LAST_DAY(c.date_base + INTERVAL 11 MONTH)
  where c.actioncd not in ('N/C','REA','SPN') AND 
        c.actioncd IS NOT NULL
  group by m.gvkey_iid, c.actioncd) as x
on 
   x.gvkey = c.gvkey AND 
   x.date_base = c.date_base
set 
   c.avg_dilution = if(x.avg_dilution is null, 0, x.avg_dilution)

价格更新: - 工作正常

update corp_act as c
inner join (
  select 
     m.gvkey, m.gvkey_iid, m.date_base, avg(m.price_dil) as avg_price
  from master as m
  inner join corp_act as c
  on c.gvkey = m.gvkey AND
     m.date_base between c.date_base AND 
                         LAST_DAY(c.date_base + INTERVAL 11 MONTH)
  where c.actioncd not in ('N/C','REA','SPN') AND 
        c.actioncd IS NOT NULL
  group by m.gvkey_iid, c.actioncd) as x
on 
   x.gvkey = c.gvkey AND 
   x.date_base = c.date_base
set 
   c.avg_price_dil = if(x.avg_price is null, 0, x.avg_price)

这两者之间的唯一区别是我只avg在单个函数中使用该函数一次。

有任何想法吗?谢谢你的帮助。

4

1 回答 1

3

set c.avg_price_dil = if(x.avg_price is null, 0, x.avg_price) AND c.avg_dilution = if(x.avg_dilution is null, 0, x.avg_dilution)

尝试用AND逗号 ( ,) 替换。

http://dev.mysql.com/doc/refman/5.0/en/update.html

于 2012-06-28T07:58:46.857 回答