Mysql插入与select distinct

时间:2017-06-22 05:23:58

标签: java mysql select insert distinct

我使用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');

结果如下:

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

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行和第34行的值计数不匹配;

1 个答案:

答案 0 :(得分: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.)