问题
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