插入唯一记录,更新重复项
我有两个表,TABLESource 是我将在 TABLEDest 中插入记录的位置,
现在 TABLESource 可以包含重复字段:
id code name
2 09 abc
3 10 uu
2 09 def
3 10 rr
2 09 gh
并且我必须首先在 TABLEDest 中插入所有唯一记录,id+code 是主键(唯一)
id code name
2 09 abc
3 10 uu
现在,最后,如果 TABLESource 发现重复的 id 和名称,它必须使用 TABLESource 中找到的具有相同主键(id_name)的最新记录来更新 TABLEDest,
id code name
2 09 gh
3 10 rr
我不知道该怎么做。请帮我。谢谢 :)
I have two tables, TABLESource is where will i get the records to be inserted in TABLEDest,
now TABLESource can contain duplicate fields:
id code name
2 09 abc
3 10 uu
2 09 def
3 10 rr
2 09 gh
and i have to insert first all the unique records in the TABLEDest, the id+code is primary key (unique)
id code name
2 09 abc
3 10 uu
now, in the end, if the TABLESource found duplicate id and name, it must update the TABLEDest with the latest records found in the TABLESource with the same primary key (id_name)
id code name
2 09 gh
3 10 rr
I have no clue how to do that. Please help me. THanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议迭代源表和源中的每一行:
这是伪代码,因此直接复制和粘贴不起作用,但它应该足以让您使用您最喜欢的语言风格来实现它。
I would suggest iterating through your source table and for each row in the source:
This is psudo code, so directly copying and pasting won't work, but it should be enough for you to use your favorite flavor of language to make it happen.
由于您无法对行进行排序,因此我选择选择 min(name) 作为名称列的值。
Since you have no way to order the rows I choose to pick the min(name) as a value for the name column.