在mysql中将特定行从一个表移动到另一个表

时间:2014-02-18 07:55:34

标签: java mysql java-ee spring-mvc

我正在开发一个网络应用....其中会有一个请求注册的用户列表...我们点击接受所有细节必须从注册表移动到登录表...

 PreparedStatement ps=conn.prepareStatement("insert into login(id,FirstName,LastName,Gender,Category,Dateofbirth,Age,Address,Country,State,city,PinCode,EmailId,ContactNo,MobileNo)select * from register ");


i have to insert all data from register table to login table when i click accept.  But when i run this the whole table gets copied from register to login table...When i click accept only that particular users details must be moved.... Id must be fetched of that particular user... How to do that. Please someone help me fix this... Thanks in advance....

1 个答案:

答案 0 :(得分:0)

您应该在SQL语句中添加WHERE子句:

INSERT INTO LOGIN (id,FirstName,LastName,Gender, ...)
SELECT * FROM REGISTER WHERE ID=?

然后将id参数绑定到PreparedStatement

PreparedStatement ps=conn.prepareStatement( ... );
ps.setInt(1, id);
ps.executeUpdate();

这样您就可以限制SELECT只返回您感兴趣的行并将其输入INSERT。