日期:2014-05-18  浏览次数:20579 次

两表的联合查询问题
现在有表A与表B:

表A
Id User Value
1 aa 3
2 bb 6
3 cc 2
4 dd 3
5 ee 1

表B
UId Value
1 3
2 5
3 2
1 7
5 4

要获取表A的的数据,条件A.Id <> B.UId And A.Value <> B.Value(同一记录),这样的查询怎样写效率高?



------解决方案--------------------
SQL code

--05
select id,value from a
except
select uid,value from b

-----or

select *
from a t
where not exists (select 1 from b where uid = t.id or value = t.value)

------解决方案--------------------
SQL code

select A.* from A
inner join B on A.Id<>B.UId and A.Value <> B.Value

------解决方案--------------------
SQL code
select
 *
from
 a
where
 not exists (select 1 from b where uid = a.id or value = a.value)

------解决方案--------------------

select * from A where not exists( 
A.ID=B.UID and A.value=B.value)