什么是SQL的AVG()函数的返回类型

时间:2017-04-16 16:59:12

标签: sql jdbc types double average

我试图通过以下方式将一个表的内容复制到另一个表中:

        sql ="INSERT INTO customer (nev, quantity, frequency) SELECT "
                + "?,COUNT(?),AVG(?)"
                + "FROM tempcustomer GROUP BY ? ORDER BY COUNT(?) DESC";

        preparedStmt=conn.prepareStatement(sql);
        preparedStmt.setString(1, "nev");
        preparedStmt.setString(2, "nev");
        preparedStmt.setString(3, "pont");
        preparedStmt.setString(4, "nev");
        preparedStmt.setString(5, "nev");

        preparedStmt.executeUpdate();

我收到以下错误消息:

com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value: 'pont'
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3971)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2501)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2079)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2013)
at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5104)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1998)
at Sample.main(Sample.java:101)

我是JDBC新手,提前谢谢!

1 个答案:

答案 0 :(得分:0)

这取决于论点。正如documentation

中所述
  

SUM()AVG()函数返回精确值的DECIMAL值   参数(整数或DECIMAL)和DOUBLE值   近似值参数(FLOATDOUBLE)。

您可以将字符串参数强制转换为数字:

AVG(? + 0)

但是,'pont'之类的值将转换为0,这将包含在平均值计算中。