我如何...如何将重复的行添加为除sql中的一列之外的新行 [英] How do I...how to add duplicate row as new row except one column in sql

查看:104
本文介绍了我如何...如何将重复的行添加为除sql中的一列之外的新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表tableName是tblProduct ...



表是



ID产品客户

1 Lux Hari

2 Dove Ram

2 Pears Banu





我需要将最后两行添加到同一个表中,除了像这样的ID ...



ID产品客户

1 Lux Hari

2 Dove Ram

2 Pears Banu

3 Dove Ram

3梨Banu

I have one table tableName is tblProduct...

the table is

ID Product Customer
1 Lux Hari
2 Dove Ram
2 Pears Banu


I need to add the Last two rows into the same table except id like this...

ID Product Customer
1 Lux Hari
2 Dove Ram
2 Pears Banu
3 Dove Ram
3 Pears Banu

推荐答案

用于插入最后两个重复的行



for insert last two duplicate rows

insert into tblProduct(Product,Customer)  select Product,CustomerFrom tblProduct where id in (select max(id) from tblProduct union select max(id)-1  from tblProduct)


检查这个

check this
insert into tblProduct(id,Customer,Product)
(select TOP 2 id+1,Customer,Product
From tblProduct
group by id,Product,customer  )order by id desc


这篇关于我如何...如何将重复的行添加为除sql中的一列之外的新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆