如何判斷一個表中某一個字段內的數量是否在另一個表中的某一個字段數據內,
謝謝!
TABLE1
code ,name
100,ss
101,aa
345,bb
653,dd
table2
code,other
100, mmsdf
101, asfsf
345, asf
123, afsaf
如上表所示要查詢出table2中字段CODE中的數據在TABLE1字段CODE中沒有的數據.
結果應該為
123,afsaf
----建表
create table table1(code nvarchar(3),name nvarchar(10))
create table table2(code nvarchar(3),other nvarchar(10))
--生成测试数据
insert into table1 values (100,ss)
insert into table1 values (101,aa)
insert into table1 values (345,bb)
insert into table1 values (653,dd)
insert into table2 values (100,mmsdf)
insert into table2 values (101,asfsf)
insert into table2 values (345,asf)
insert into table2 values (123,afsaf)
--测试代码
select *
from table2
where code not in(
select code
from table1
)
--运行结果
code other
123 afsaf
方法2
select *
from table2 a
where not exists (
select *
from table1 b
where a.code=b.code
)