获取相关表postgres之间至少一个重合的值



我有3个表:

CREATE TABLE public.student (id serial NOT NULL PRIMARY KEY,"student" varchar(30) NOT NULL) WITH (OIDS = FALSE);
  CREATE TABLE public."class" (id serial NOT NULL PRIMARY KEY,"class" varchar(30) NOT NULL) WITH (OIDS = FALSE);

 CREATE TABLE public.student_class (student_id integer, class_id integer, /* Keys */ CONSTRAINT student_class_index01
    PRIMARY KEY (student_id, class_id)) WITH (OIDS = FALSE);

INSERT INTO public.student_class ("student_id","class_id") VALUES(1,1),(1,2),(1,3),(1,4),(1,5),(1,6),(1,7),(2,1),(2,2),(2,3),(3,4),(3,5),(3,6),(3,7),(3,8);

我想知道至少有一个班级与另一个班级重合的学生的名字:

学生1与2和3至少有一次重合。学生2至少有一个与1相符。学生3至少得到了一个1.

我已经试过了,但是它不是由学生做的。

SELECT distinct a.id,a.student from public.student a
LEFT OUTER JOIN public.student_class b ON b.student_id=a.id
LEFT OUTER JOIN public.class d ON d.id=b.class_id
WHERE class_id IN(select class_id from student_class where student_id not in(1))
AND a.id not in(1);

我认为这比我想象的要容易。谢谢。

你怎么看:

with student_lst_classes as (
    select student_id,array_agg(class_id order by class_id) lst_classes
      from student_class  group by 1
 ) 
select st1.student,string_agg(st2.student,';' order by st2.student ASC) lst_student_coincident from 
 (student_lst_classes l1 join student st1 on (st1.id=l1.student_id)) 
 ,(student_lst_classes l2 join student st2 on (st2.id=l2.student_id))
where l1.student_id!=l2.student_id and l1.lst_classes && l2.lst_classes
group by 1 order by 1

试试这个:

select *
from   student
where  exists (select null
                 from student_class sc1
                where sc1.student_id = student.id
                  and exists (select null
                                from student_class sc2
                               where sc2.class_id   =  sc1.class_id
                                 and sc2.student_id != sc1.student_id))

基本上是student X student的笛卡尔连接,所以没有重复的需要注意。(除了对称的s1 <--> s2)

SELECT s1.id AS a_id, s1.student AS a_student
        , s2.id AS b_id, s2.student AS b_student
FROM student s1
JOIN student s2 ON EXISTS (
        SELECT *
        FROM student_class x1
        JOIN student_class x2 ON x2.class_id = x1.class_id
        WHERE x1.student_id = s1.id
        AND x2.student_id = s2.id
        )
WHERE s1.id < s2.id -- tie-breaker
        ;

相关内容

最新更新