插入唯一记录,更新重复项

发布于 2024-12-06 05:49:56 字数 492 浏览 0 评论 0原文

我有两个表,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小红帽 2024-12-13 05:49:56

我建议迭代源表和源中的每一行:

Psudo Code:
//Get all source rows
query = 'SELECT * FROM TABLEsource'
rows = database.get(query)

//Iterate through all source rows
for each rows as row{
    //Search dest table for a match    
    checkQuery = 'SELECT * FROM TABLEDest WHERE id = '+row.id
    check = database.get(checkQuery)

    //If we do get a match
    if(check.rows > 0){

       //Update the relevant row
       query = 'UPDATE TABLEDest SET name = '+row.name+' WHERE id = '+check.id
       database.query(query)

    }else{

       //Otherwise, append a new row to the dest table
       query = 'INSERT INTO TABLEDest(id, code, name) '
              +'VALUES('+row.id+'. '+row.code+', '+row.name+')'
       database.query(query)

    }
}

这是伪代码,因此直接复制和粘贴不起作用,但它应该足以让您使用您最喜欢的语言风格来实现它。

I would suggest iterating through your source table and for each row in the source:

Psudo Code:
//Get all source rows
query = 'SELECT * FROM TABLEsource'
rows = database.get(query)

//Iterate through all source rows
for each rows as row{
    //Search dest table for a match    
    checkQuery = 'SELECT * FROM TABLEDest WHERE id = '+row.id
    check = database.get(checkQuery)

    //If we do get a match
    if(check.rows > 0){

       //Update the relevant row
       query = 'UPDATE TABLEDest SET name = '+row.name+' WHERE id = '+check.id
       database.query(query)

    }else{

       //Otherwise, append a new row to the dest table
       query = 'INSERT INTO TABLEDest(id, code, name) '
              +'VALUES('+row.id+'. '+row.code+', '+row.name+')'
       database.query(query)

    }
}

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.

橘寄 2024-12-13 05:49:56

由于您无法对行进行排序,因此我选择选择 min(name) 作为名称列的值。

insert into TABLEDest
select id, code, min(name)
from TABLESource
group by id, code

Since you have no way to order the rows I choose to pick the min(name) as a value for the name column.

insert into TABLEDest
select id, code, min(name)
from TABLESource
group by id, code
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文