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
当前回答
我将在正确答案....中转发(因为我还没有酷到可以评论)以防有人觉得需要更好的解释。
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中,它似乎更喜欢空格。
底线是,当你使用不好的变量名时,它会留下问题。我的变量应该更有意义。应该有人解释一下为什么我们需要逗号或不需要逗号。
其他回答
你可以选择
SELECT name
FROM table2
WHERE name NOT IN
(SELECT name
FROM table1)
or
SELECT name
FROM table2
WHERE NOT EXISTS
(SELECT *
FROM table1
WHERE table1.name = table2.name)
请看这个问题的3个技巧来完成这个任务
这是纯集合论你可以用减号运算来实现。
select id, name from table1
minus
select id, name from table2
我将在正确答案....中转发(因为我还没有酷到可以评论)以防有人觉得需要更好的解释。
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)
SELECT <column_list>
FROM TABLEA a
LEFTJOIN TABLEB b
ON a.Key = b.Key
WHERE b.Key IS NULL;
https://www.cloudways.com/blog/how-to-join-two-tables-mysql/