« 上一篇 | 下一篇»

关于关系代数 R 除 S 的SQL 写法

songhefei | 03 04, 2007, 21:11 | oracle develop | (13923 Reads)

例如:

R (A,B):中的元组

AB
ab
ad
fc

S(B):中的元组

B
b
d

R div S 的元组为

A
a

SQL的写法:

1.

select T.c1 from

(select r1.A as c1,r1.B as c2,r2.B as c3 from R r1,R r2 where r1.A=r2.A) T

where T.c2 in (select S.B from S)

and T.c3 in (select S.B from S) and T.c2<>T.c3

这里R的自身连接次数取决于S中的元组数量.

2.更简单的方法:

select R.A,count(distinct R.B) from R group by R.B having count(distinct R.B)>=(select coutn(distinct S.B) from S)


Trackback URL: http://blog.itpub.net//trackback.php?id=277124
Comments