pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tomasz Myrta <jasiek@klaster.net>
To: email_daniel_h@yahoo.com.br
Cc: pgsql-sql@postgresql.org
Subject: Re: Help to simplify sample query
Date: Tue, 09 Mar 2004 07:24:52 +0100
Message-ID: <404D6334.4030501@klaster.net> (raw)
In-Reply-To: <404D5920.40104@yahoo.com.br>
References: <404D5920.40104@yahoo.com.br>

Dnia 2004-03-09 06:41, Użytkownik Daniel Henrique Alves Lima napisał:
>    Hi guys, i have a new question about how to simplify a query. I have 
> the tables area_course(cd_area,cd_course) and 
> teacher_course(cd_teacher,cd_course) and a set of pairs of values 
> {(1,2),(98,45),(11,0),...}.
> 
>    Now, i must to select the areas which courses appears in 
> teacher_course and match with the set of pairs. Something like :
> 
> select cd_area from area a1 where
>   exists (select * from teacher_course c2 where c2.cd_course = 
> a1.cd_course and c2.cd_teacher = 1 and c2.cd_course = 2) and
>   exists (select * from teacher_course c2 where c2.cd_course = 
> a1.cd_course and c2.cd_teacher = 98 and c2.cd_course = 45) and
>   exists (select * from teacher_course c2 where c2.cd_course = 
> a1.cd_course and c2.cd_teacher = 11 and c2.cd_course = 0) and
> ....
> 
>    This is just a sample. The whole query is giant and its use other 
> tables/columns. Is there a best way to write this query ?

Can you try this query ? :

select cd_area from area a1
  join teacher_course c2 using (cd_course)
where (cd_teacher,cd_course) in (select 1,2 union select 98,45 union 
select 11,0);

Regards,
Tomasz Myrta



view thread (7+ messages)  latest in thread

Message-ID: <404D6334.4030501@klaster.net>
Permalink:  ../404D6334.4030501@klaster.net/
Also on:    postgresql.org/message-id/404D6334.4030501@klaster.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: jasiek@klaster.net, email_daniel_h@yahoo.com.br
  Subject: Re: Help to simplify sample query
  In-Reply-To: <404D6334.4030501@klaster.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox