我尝试了以下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
函数一次。
有什么想法吗? 谢谢你的帮助。
答案 0 :(得分: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
)替换,
。