忽略两个表sql server中存在的记录

时间:2015-04-28 07:49:17

标签: sql sql-server sql-server-2008-r2

表1

ID  Department     Date
555    67         2015-04-28
111    58         2015-04-28
000    45         2015-04-28

表2

ID    DEpartment       Category
888     16               test
0001    16               test

如果我必须传入参数值table1.department = 16,我该如何创建存储过程它必须从table1中检索所有记录,但如果id在表2中,则必须忽略该记录。

如果我传递参数table1.department = 16

预期输出

输出

select *
from table1 as t1 inner join table2 as t2 on t1.ID=t2.ID
where t1.department='16'

id 555应该被忽略。

尝试了什么

from itertools import tee

def unpack_args(fn):
    return lambda t: fn(*t)

def separate(fn, lx):
    return map(
        unpack_args(
            lambda i, ly: filter(
                lambda el: bool(i) == fn(el),
                ly)),
        enumerate(tee(lx, 2)))

1 个答案:

答案 0 :(得分:2)

此查询将返回您想要的结果

SELECT Table1.ID, 
        Table1.Dept,
        Table1.Category 
FROM table1 WHERE Table1.ID NOT IN (SELECT Table2.ID FROM table2)