怎么样用sql语句查出它——

来源:百度知道 编辑:UC知道 时间:2024/06/28 04:23:47
表1:
列A 列B
aa 10
bb 61
cc 71
dd 34
ee 57
表2:
列C 列D
aa I
bb II
dd I
ee I
要求执行完sql后结果为
bb
cc
即找出不对应着“I”的表1的列A的值。说得更清楚点,就是。。凡是不属于I的,都给俺找出来。虽然bb在表2中,但它不属于I,也要找出来。

select 列A from 表1 where 列A not in (select 列C from 表2)
union
select 列A from 表1 where 列A in(select 列C from 表2 where 列D <> 'II')
order by 1;

---
以上,希望对你有所帮助。

--表1:
create table #1(A char(10),B char(10))
go
insert #1 select
'aa', '10' union all select
'bb', '61' union all select
'cc','71' union all select
'dd','34' union all select
'ee', '57'
go
--表2:
create table #2(C char(10),D char(10))
go
insert #2 select
'aa' ,'I' union all select
'bb' , 'II' union all select
'dd', 'I' union all select
'ee' , 'I'

go

--测试
select A from #1 where A not in (select C from #2)
union
select C from #2 where D <> 'I'
order by #1.A

select id, name
from ta