在聚合函数上插入选择查询

时间:2016-02-21 03:36:56

标签: mysql

参考以下主题: 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);

1 个答案:

答案 0 :(得分:0)

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