参考以下线程:
Can you use aggregate values within ON DUPLICATE KEY

我尝试复制该方法以及在ma20上插入值的许多其他方法,但没有任何效果,并且不断出现以下错误:
错误代码:1054。“字段列表”中的未知列“ histClose”

我做错了什么,该如何做?

insert into moving_average (ma_symbol, ma_date, ma20)
select 'A', max(histDate) as maxHistDate, avg(histClose) as histClose
from
   (select histDate, histClose
   from historical_data
   where symbol like 'A'
   order by histDate asc
   limit 20) temp
   on duplicate key update ma20 = values(histClose);

最佳答案

values关键字采用表中列的名称。 。 。它是将进入列的值:

insert into moving_average (ma_symbol, ma_date, ma20)
    select 'A', max(histDate) as maxHistDate, avg(histClose) as histClose
    from (select histDate, histClose
          from historical_data
          where symbol like 'A'
          order by histDate asc
          limit 20
         ) temp
    on duplicate key update ma20 = values(ma20);

关于mysql - 在聚合函数上插入带有选择查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35531985/

10-09 01:28