How to select a subset of results from a select statement

Posted by Ankur on Stack Overflow See other posts from Stack Overflow or by Ankur
Published on 2010-03-30T14:46:03Z Indexed on 2010/03/30 15:03 UTC
Read the original article Hit count: 179

Filed under:

I have a table that stores RDF triples:

triples(triple_id, sub_id, pre_id, obj_id)

The method (I need to write) will receive an array of numbers which correspond to pre_id values. I want to select all sub_id values that have a corresponding pre_id for all the pre_ids in the array that is passed in.

E.g. if I had a single pre_id values passed in... lets call the value passed in preId, I would do:

select sub_id from triples where pre_id=preId;

However since I have mutliple pre_id values I want to keep iterating through the pre_id values and only keep the sub_id values corresponding to the "triples" records that have both.

E.g. image there are five records:

triples(1, 34,65,23)
triples(2, 31,35,28)
triples(3, 32,32,19)
triples(4, 12,65,28)
triples(5, 76,32,34)

If I pass in an array of pre_id values [65,32] then I want to select the first, third, fourth and fifth records.

What would I do for that?

© Stack Overflow or respective owner

Related posts about sql