2017-06-22 83 views
0

我使用的Java和MySQL,我有在MySQL的代码Mysql的插入使用select distinct

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薄荷......”是未知的,但如果我使用选择进入插入

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匹配值计数“

+0

有什么'climdatas'表的定义是什么?除了要插入的列以外是否还有其他列? –

+0

,id,hightemp,lowtemp,avgtemp,highhum,lowhum,avghum,highpress,lowpress,avgpress,dateinsert,zonedatas但id自动增加,我用数值测试 – lina20

+0

在这种情况下,尝试将第一个插入语句与其次 - 主要包括插入列的列表。猜测,它也试图插入'id'列。 –

回答

1

好像你有多于或少于11列的climdatas表。在你的插入语句中,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.) 
+0

我已经使用了这个语句和同样的问题,因为结果表在第一列给出'datetemp',就像上面的表和'climdatas'一样,'dateinsert'列在最后一列之前 – lina20

+0

然后更改列的顺序。你可以做到这一点。查看更新后的答案。不要更改您的选择查询。保持原样。 –

+0

当然。欢迎。 :) –