【问题标题】:Mysql insert into with select distinctMysql insert into with select distinct
【发布时间】:2017-11-25 05:21:19
【问题描述】:

我正在使用 java 和 mysql,我在 mysql 中有这段代码

SELECT DISTINCT date(datecreat) as datetemp , (SELECT max(sensortemp) FROM akdb.iotdatas WHERE date(datecreat)=datetemp ) as maxt , (SELECT min(sensortemp) FROM akdb.iotdatas WHERE date(datecreat)=datetemp ) as mint , (SELECT avg(sensortemp) FROM akdb.iotdatas WHERE date(datecreat)=datetemp ) as avgt , (SELECT max(sensorhum) FROM akdb.iotdatas WHERE date(datecreat)=datetemp ) as maxh , (SELECT min(sensorhum) FROM akdb.iotdatas WHERE date(datecreat)=datetemp ) as minh , (SELECT avg(sensorhum) FROM akdb.iotdatas WHERE date(datecreat)=datetemp ) as avgh , (SELECT max(sensorpres) FROM akdb.iotdatas WHERE date(datecreat)=datetemp ) as maxp , (SELECT min(sensorpres) FROM akdb.iotdatas WHERE date(datecreat)=datetemp ) as minp , (SELECT avg(sensorpres) FROM akdb.iotdatas WHERE date(datecreat)=datetemp ) as avgp FROM akdb.iotdatas where (serialnum ='0000000000000');

结果是这样的:

但我想使用此代码将此数据插入到其他表中

insert into akdb.climdatas(hightemp,lowtemp,avgtemp,highhum,lowhum,avghum,highpress,lowpress,avgpress,dateinsert,zonedatas)values(maxt,mint,avgt,maxh,minh,avgh,maxp,minp,avgp,datetemp,'zone h');

这里的问题是“maxt mint ...”是未知的,但是如果我使用 select into insert

insert into akdb.climdatas(SELECT DISTINCT date(datecreat) as datetemp ,(SELECT max(sensortemp) FROM akdb.iotdatas WHERE date(datecreat)=datetemp ) , (SELECT min(sensortemp) FROM akdb.iotdatas WHERE date(datecreat)=datetemp ) , (SELECT avg(sensortemp) FROM akdb.iotdatas WHERE date(datecreat)=datetemp ) , (SELECT max(sensorhum) FROM akdb.iotdatas WHERE date(datecreat)=datetemp ) , (SELECT min(sensorhum) FROM akdb.iotdatas WHERE date(datecreat)=datetemp ) , (SELECT avg(sensorhum) FROM akdb.iotdatas WHERE date(datecreat)=datetemp ) , (SELECT max(sensorpres) FROM akdb.iotdatas WHERE date(datecreat)=datetemp ) , (SELECT min(sensorpres) FROM akdb.iotdatas WHERE date(datecreat)=datetemp ) , (SELECT avg(sensorpres) FROM akdb.iotdatas WHERE date(datecreat)=datetemp ),' zone h'FROM akdb.iotdatas where (serialnum ='0000000000000000'))

错误出现在“mysql 列计数与第 1 行的值计数不匹配”列中

【问题讨论】:

  • climdatas 表的定义是什么?除了要插入的列之外,还有其他列吗?
  • ,id,hightemp,lowtemp,avgtemp,highhum,lowhum,avghum,highpress,lowpress,avgpress,dateinsert,zonedatas 但 id 会自动递增,我已经用值进行了测试
  • 在这种情况下,尝试将第一个插入语句与第二个结合起来——基本上包括要插入的列列表。猜测一下,它也在尝试插入id 列。
  • 如何将`datetemp` 列从第一列更改为最后一列??
  • 查看 Keyur Panchal 的答案,您只需将列表中的列移动到您期望数据所在的位置。

标签: java mysql select insert distinct


【解决方案1】:

climdatas 表中的列似乎多于或少于 11 列。在您的插入语句中,specify list of columns separated by comma

insert into akdb.climdatas (dateinsert,hightemp,lowtemp,avgtemp,highhum,lowhum,avghum,highpress,lowpress,avgpress,zonedatas)
(your select query goes here.)

【讨论】:

  • 我使用了这个语句和同样的问题,因为结果表在第一列给出了'datetemp',就像上面的表和'climdatas','dateinsert'列在最后一个之前
  • 然后改变列的顺序。你可以这样做。请参阅更新的答案。不要更改您的选择查询。保持原样。
  • 当然。欢迎。 :)
  • 如果我不想重复日期怎么办
  • 我没有得到你。请您再描述一下好吗?
猜你喜欢
  • 1970-01-01
  • 2013-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多