插入子查询和唯一值

时间:2016-02-16 06:11:44

标签: mysql

我想使用select语句和唯一值插入表中。以下查询返回列计数不匹配的错误。我错过了什么?

insert into moving_average (ma_symbol, ma_date, ma5)
values 
 (
      select 'A', max(temp.histDate), avg(temp.histClose)
      from
         (
          select hd.histDate, hd.histClose
          from historical_data hd
          where symbol like 'A' order by hd.histDate asc limit 5
         ) temp
  );

1 个答案:

答案 0 :(得分:0)

试试这个:

insert into moving_average (ma_symbol, ma_date, ma5)
select 'A', max(histDate), avg(histClose)
from (select (histDate), (histClose)
      from historical_data
      where symbol like 'A'
      order by histDate asc
      limit 5) temp;

INSERT INTO ... SELECT语句不使用VALUES关键字。