gpt4 book ai didi

java - 如何使用MySQL通过preparedStatement将数据从一张表复制到另一张表?

转载 作者:行者123 更新时间:2023-11-29 19:02:19 24 4
gpt4 key购买 nike

我有以下代码:

String sql ="INSERT INTO table_a (some_num, text, popularity) "
+ "SELECT numero, text, count(text) FROM table_b GROUP BY numero, text";
PreparedStatement pstm = conn.prepareStatement(sql);
pstm.executeUpdate();

table_a 是我的 MySQL 服务器中的一个表,具有以下结构:some_num INT(11) NOT NULL、text VARCHAR(255)、受欢迎度 INT(11);

table_b 是我的代码中的临时表,其中 numero INT NOT NULL,文本 VARCHAR(255)

我想将 table_b 中的所有行插入到 table_a 中,但最终出现以下错误跟踪:

SEVERE: null
java.sql.SQLException: invalid new some_num data
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
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 Tester.main(Tester.java:67)

最佳答案

String sql ="INSERT INTO table_a (some_num, text, popularity) "
+ "SELECT numero, text, count(text) table_b GROUP BY text";
PreparedStatement pstm = conn.prepareStatement(sql);
pstm.executeUpdate();

第一列应该是什么?您的结果集是按文本而不是数字分组的,因此您无法获取数字列。

关于java - 如何使用MySQL通过preparedStatement将数据从一张表复制到另一张表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43771698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com