0

有人可以指导在postgresql中使用带有语法支持的排序来实现关系代数除法运算符吗?编辑:这是处理postgresql的源代码。我需要在 Postgresql 中添加除法功能。

4

1 回答 1

1

不确定您对排序语法支持的含义,但关系除法运算符似乎只是一个复杂的连接:

select a_.id
from (select a.id, array_agg(a.b_id) as b_ids from a group by a.id) as a_
join (select array_agg(b.id) as b_ids from b) as b_ on b_.b_ids <@ a_.b_ids

http://en.wikipedia.org/wiki/Relational_algebra#Division_.28.C3.B7.29


denis=# create table a (id int, b_id int);
CREATE TABLE
denis=# create table b (id int);
CREATE TABLE
denis=# insert into a values (1,1), (1,2), (1,3), (2,1), (2,3), (3,1), (3,2);
INSERT 0 7
denis=# insert into b values (1), (2);

denis=# select a_.id from (select a.id, array_agg(a.b_id) as b_ids from a group by a.id) as a_ join (select array_agg(b.id) as b_ids from b) as b_ on b_.b_ids <@ a_.b_ids;
 id 
----
  1
  3
(2 rows)
于 2013-11-08T18:55:02.970 回答