Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: IN clause but needs to be 'AND'

Re: IN clause but needs to be 'AND'

From: Billy Verreynne <vslabs_at_onwe.co.za>
Date: Tue, 04 Feb 2003 13:17:34 +0000
Message-ID: <b1o7h8$kgn$1@ctb-nnrp2.saix.net>


Jeremy Taylor wrote:

> A 'route' is made up of a list of 'segments'. We have a table that
> captures this called 'ROUTE_SEGMENTS'. It lists all the 'segments' in
> a 'route' (it's a many-to many join table).
>
> We can easily find all the routes that use any one of a list of
> segments by using 'IN':
>
> select route_id from route_segments where segment_id in
> (100,200,300,400);
>
> but how do I find all the routes that use ALL of the segments in my
> list? (in other words I want to find all the routes that pass over a
> given list of segments)

Unless I'm missing something (like my brain and/or more caffeine), you can do this as follows:

/* get all segments for the list of specified routes */ SELECT
  DISTINCT segment_id
FROM route_segment
WHERE route_id IN
(
  /* get a distinct list of routes using the specified segments */   SELECT
    DISTINCT route_id
  FROM route_segments
  WHERE segment_id IN (100,200,300,400)
)  

--
Billy
Received on Tue Feb 04 2003 - 07:17:34 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US