[英]How can I select rows from a table that don't exist in another table?
I have 2 tables - first table holds all unique data (columns: title, artist, album). 我有2个表-第一个表包含所有唯一数据(列:标题,艺术家,专辑)。 The second table has repeated data or people listening daily to an rss feed. 第二个表有重复的数据,或者每天都有人在收听RSS提要。
I want to save all the data from table2 to table1, but only if the row of table2 doesn't exist in table1. 我想将所有数据从table2保存到table1,但前提是table1中不存在table2的行。 I want a sql query which will return all rows of table2 that aren't in table1 - how? 我想要一个SQL查询,该查询将返回table2中没有的所有table2行-怎么办?
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: 假设表2中存在title, artist, album
列title, artist, album
并且您想要添加表2中所有这三行的给定组合在表1中不存在的所有行,则可以执行以下操作:
Insert Table1( title, artist, album, ... )
Select title, artist, album, ...
From Table2
Where ( title, artist, album ) Not In (
Select title, artist, album
From Table1
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.