开发者

How can I select rows from a table that don't exist in another table?

开发者 https://www.devze.com 2023-02-22 02:02 出处:网络
I have 2 tables - first table holds all unique data (colu开发者_高级运维mns: title, artist, album). The second table has repeated data or people listening daily to an rss feed.

I have 2 tables - first table holds all unique data (colu开发者_高级运维mns: title, artist, album). The second table has repeated data or people listening daily to an rss feed.

I want to save all the data from table2 to table1, but only if the row of table2 doesn't exist in table1. I want a sql query which will return all rows of table2 that aren't in table1 - how?


Something like this, probably:

INSERT INTO Table1
(columns)
SELECT columns
FROM Table1
WHERE Table2.UniqueColumn NOT IN (SELECT UniqueColumn FROM Table1)

?


Assuming that the columns title, artist, album exist in Table2 and that you want to add all rows from Table2 where the given combination of those three do not exist in Table1, you can do something like:

Insert Table1( title, artist, album, ... )
Select title, artist, album, ...
From Table2
Where ( title, artist, album ) Not In   (
                                        Select title, artist, album
                                        From Table1
                                        )
0

精彩评论

暂无评论...
验证码 换一张
取 消