如何将table1中的数据复制到table2中

1、当table1和table2的字段完全相同时
  字段数很多,懒得全列出来时:
  SQL:inster into table2 select * from table1;
  字段数很少
  SQL: inster into table2(id,name) select a.id,a.name from table1 a;
  
2、当table2字段比table1多时
  字段很少的情况下还是按上面的方法全列出来
  字段很多时
  SQL: inster into table2 select a.*,?,? from table1 a;  //问号处可插入变量或者常量
  或者关联其他表数据一起插入
  SQL: inster into table2 select a.*,b.flag,b.time from table1 a left join table2 b on a.id=b.id;  //类似这样,这个例子只是为了补足位置,所以插的值是空值
int flag=1;
String no="123";
StringBuffer sql = new StringBuffer();
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = dateFormat.format(now);
sql.append("insert into table2 select a.*,?,? from table1 a where a.no=?");
SQLQuery query = this.getSession().createSQLQuery(hql.toString());
query.setInteger(0, flag);
query.setString(1, time);
query.setString(2, no);
query.executeUpdate();
这是我在hibernate框架中的代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值