Table1 (id, name) Table2 (id, name)
查询:
SELECT name
FROM table2
-- that are not in table1 already
Table1 (id, name) Table2 (id, name)
查询:
SELECT name
FROM table2
-- that are not in table1 already
当前回答
你可以在mssql中使用EXCEPT或在oracle中使用MINUS,它们是相同的:
http://blog.sqlauthority.com/2008/08/07/sql-server-except-clause-in-sql-server-is-similar-to-minus-clause-in-oracle/
其他回答
你可以在mssql中使用EXCEPT或在oracle中使用MINUS,它们是相同的:
http://blog.sqlauthority.com/2008/08/07/sql-server-except-clause-in-sql-server-is-similar-to-minus-clause-in-oracle/
那对我来说很有用
SELECT *
FROM [dbo].[table1] t1
LEFT JOIN [dbo].[table2] t2 ON t1.[t1_ID] = t2.[t2_ID]
WHERE t2.[t2_ID] IS NULL
首先定义表的别名,如t1和t2。 然后得到第二个表的记录。 然后使用where条件匹配记录:
SELECT name FROM table2 as t2
WHERE NOT EXISTS (SELECT * FROM table1 as t1 WHERE t1.name = t2.name)
我将在正确答案....中转发(因为我还没有酷到可以评论)以防有人觉得需要更好的解释。
SELECT temp_table_1.name
FROM original_table_1 temp_table_1
LEFT JOIN original_table_2 temp_table_2 ON temp_table_2.name = temp_table_1.name
WHERE temp_table_2.name IS NULL
在mySQL中,我看到FROM中的语法需要在表名之间使用逗号,但在sqlLite中,它似乎更喜欢空格。
底线是,当你使用不好的变量名时,它会留下问题。我的变量应该更有意义。应该有人解释一下为什么我们需要逗号或不需要逗号。
小心陷阱。如果表1中的字段Name包含null,你就会感到惊讶。 更好的是:
SELECT name
FROM table2
WHERE name NOT IN
(SELECT ISNULL(name ,'')
FROM table1)