简体   繁体   English

如何从一个表中选择另一个表中不存在的行?

[英]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, albumtitle, 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.

相关问题 如何将行从一个表移动到另一个表中不存在的行? - How can I move rows from one table to another where they don't exist in a third? 如果表 B 中的行不存在,如何从表 A 和表 B 中删除行 - How can I delete rows from table A and table B if rows in table B don't exist 如果其他表中的行不存在,请插入它们 - Insert rows if they don't exist into a table from another table 从一个表中选择记录,这些记录在另一个表中不存在 - Select records from a table, which don't exist in another table 如何从另一个表中没有特定值的UID的表中选择*? - How can I select * from a table where a UID doesn't exist with a certain value in another table? 如何从一个SQL表中选择未出现在另一表中的项目 - How can I select items from one sql table that don't appear in another table 如何在MySQL的另一个表中仅选择没有相应外键的行? - How do I select only rows that don't have a corresponding foreign key in another table in MySQL? SQL:选择第二个表中不存在的相似行 - SQL: select similar and rows that don't exist in second table 从表中删除另一个表中不存在的所有记录 - Removing all records from a table that don't exist in another table 选择另一个数据库表中不存在的记录 - Select records that don't exist in another DB table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM