在SQLite中,如何在一个表中选择不在其他表中的元素?

bod*_*ydo 3 sql sqlite

我在SQLite中有两个表:

Table1:
-------
id
name

Table2:
-------
id
temp_name
Run Code Online (Sandbox Code Playgroud)

我的问题是,如何编写一个返回Table2不存在的名称的SQL查询Table1

例如:

Table1:
-------
1, 'john'
2, 'boda',
3, 'cydo',
4, 'linus'

Table2:
-------
1123, 'boda'
2992, 'andy',
9331, 'sille',
2,    'cydo'
Run Code Online (Sandbox Code Playgroud)

在这个例子中,SQL查询应返回的元素andy,并silleTable2,因为他们不是Table1.

Gor*_*off 9

这是在"明显的"标准SQL中如何做到这一点:

select *
from table2
where temp_name not in (select name from table1)
Run Code Online (Sandbox Code Playgroud)

还有其它方法,如使用left outer join,existswhere子句和except操作.


RFo*_*Fox 5

我很好奇哪些选项最适合我的用例,并认为如果我分享我的结果可能会对其他人有所帮助:

简而言之,选择 not in速度最快的地方(对我来说!)。还值得注意的是,使用除except之外的所有选项都会返回重复项。

Option 0: SELECT {c2} FROM {t2} WHERE {c2} not in (SELECT {c1} FROM {t1});
    Entries returned: 1098, Unique entries returned: 357
    Average: 1.8680 seconds
    Entries returned: 0, Unique entries returned: 0
    Average: 0.6664 seconds
Option 1: SELECT {c2} FROM {t2} EXCEPT SELECT {c1} FROM {t1};
    Entries returned: 357, Unique entries returned: 357
    Average: 3.9455 seconds
    Entries returned: 0, Unique entries returned: 0
    Average: 3.3074 seconds
Option 2: SELECT {t2}.{c2} FROM {t2} LEFT OUTER JOIN {t1} ON {t1}.{c1} = {t2}.{c2} WHERE {t1}.{c1} IS null;
    Entries returned: 1098, Unique entries returned: 357
    Average: 2.3330 seconds
    Entries returned: 0, Unique entries returned: 0
    Average: 1.1982 seconds
Option 3: SELECT {c2} FROM {t2} WHERE NOT EXISTS (SELECT 1 FROM {t1} WHERE {c1} = {t2}.{c2});
    Entries returned: 1098, Unique entries returned: 357
    Average: 2.6945 seconds
    Entries returned: 0, Unique entries returned: 0
    Average: 0.9737 seconds
Run Code Online (Sandbox Code Playgroud)

这是我用来运行数字的代码:

import sqlite3
import timeit

# Database path here
database = "database.db"

# Your table and column names here
t1, c1 = 'Table_1', 'name'
t2, c2 = 'Table_2', 'temp_name'

# Reverse the test
dbs = [{'t1': t1, 'c1':c1, 't2': t2, 'c2': c2},
       {'t1': t2, 'c1':c2, 't2': t1, 'c2': c1}]

commands = ["SELECT {c2} FROM {t2} WHERE {c2} not in (SELECT {c1} FROM {t1});",
"SELECT {c2} FROM {t2} EXCEPT SELECT {c1} FROM {t1};",
"SELECT {c2} FROM {t2} LEFT OUTER JOIN {t1} ON {t1}.{c1} = {t2}.{c2} WHERE {t2}.{c2} IS null;",
"SELECT {c2} FROM {t2} WHERE NOT EXISTS (SELECT 1 FROM {t1} WHERE {c1} = {t2}.{c2});",]

for i, c in enumerate(commands):
    print("Option {}: {}".format(i, c))
    for db in dbs:
        co = c.format(**db)
        foo = sqlite3.connect(database).execute(co).fetchall()
        # Sanity check that entries have been found and how many
        print("\tEntries returned: {}, Unique entries returned: {}".format(len(foo), len({a[0] for a in foo})))
        # Reconnect to the database each time - I can't remember if there's any caching
        t = timeit.repeat(lambda: sqlite3.connect(database).execute(co).fetchall(), repeat=5, number=1)
        print('\tAverage: {:.4f} seconds'.format(statistics.mean(t)))
Run Code Online (Sandbox Code Playgroud)