--主表:
bindid NO
123 111
--子表:
bindid NO
123 111
123 111
123 111
321 222
321 222
321 222
312 333
将没有binid关联的筛选出来,结果是:
bindid NO
321 222
321 222
321 222
312 333
sql语句怎么写?
------解决方案--------------------
select * from 子表 a
where not exists (select 1 from 主表 b where a.bindid=b.bindid)
------解决方案--------------------
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2014-02-18 14:15:33
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[主表]
if object_id('[主表]') is not null drop table [主表]
go
create table [主表]([bindid] int,[NO] int)
insert [主表]
select 123,111
--> 测试数据:[子表]
if object_id('[子表]') is not null drop table [子表]
go
create table [子表]([bindid] int,[NO] int)
insert [子表]
select 123,111 union all
select 123,111 union all
select 123,111 union all
select 321,222 union all
select 321,222 union all
select 321,222 union all
select 312,333
--------------开始查询--------------------------
select * from [子表] a
WHERE bindid NOT IN (SELECT bindid FROM [主表])
--select * from [主表]
----------------结果----------------------------
/*
bindid NO
----------- -----------
321 222
321 222
321 222
312 333
*/