How to copy row from a table to another table if the entry is not exist in the new table in sql [closed]

人走茶凉 提交于 2019-12-12 12:08:58

问题


I need to copy all data from a table to an another same schema table. If the new table has a row already then i need not to update or insert it. I need only new data from old table that does not exist in new table to copy to that new table.

So what will be the sql query, can any one help me.

Thanks, Muntasir Rahman Rafi


回答1:


try this :

INSERT INTO TABLENAME2 (col1, Col2, col3)
SELECT *
FROM tablename1 A
WHERE NOT EXISTS (
          SELECT *
          FROM tablename2 B
          WHERE A.col1 = B.col1
     )



回答2:


Try this one -

INSERT INTO new_table(...)
SELECT *
FROM old_table AS o
WHERE o.ID NOT IN (
     SELECT n.ID FROM new_table AS n
)

Or this -

INSERT INTO new_table(...)
SELECT o.*
FROM old_table AS o
LEFT JOIN new_table AS n ON n.ID = o.ID
WHERE n.ID IS NULL


来源:https://stackoverflow.com/questions/17540598/how-to-copy-row-from-a-table-to-another-table-if-the-entry-is-not-exist-in-the-n

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!