dpfps86064 2016-07-04 11:03
浏览 19
已采纳

只从一个表上传新行到另一个表

Hi I know how to upload data from one table to another one:

INSERT INTO new_table (id, pricerange, rentrange, date)
SELECT id, price, rent, date
FROM initial_table

But I have a problem with this if you consider I have 10 rows in my initial table by this code I can upload all of them into new table and then after for example 10 hours if I have 10 new rows and I use this code in my new table I will have 30 rows. Because this code did not delete 10 old row and also add all 20 rows again. what can I do that not to upload first 10 row again?

  • 写回答

3条回答 默认 最新

  • dsk710351 2016-07-04 11:38
    关注

    Hey you can use this query. This will insert only those row which are not present in first table

      INSERT INTO new_table(id, pricerange, rentrange, date) SELECT id, price, rent, date  FROM initial_table WHERE NOT EXISTS(SELECT * FROM new_table WHERE (initial_table.id=new_table.id))  
    

    You can add more condition in if statement if you want

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?