Re: Matching several rows - Mailing list pgsql-sql

From Michael Glaesemann
Subject Re: Matching several rows
Date
Msg-id DBEB2BBA-60E7-41FF-B409-D95E605B8BB0@myrealbox.com
Whole thread Raw
In response to Matching several rows  (Ivan Steganov <istegan6@gmail.com>)
List pgsql-sql
On Jan 18, 2006, at 13:43 , Ivan Steganov wrote:

> A table URIGHTS which stores an ID and the RIGHTs this ID has. One  
> ID may have many rights and accordingly records in table, sample:

> Now I need to find out which IDs have, say rights 2 AND 5 AND 10.

select "ID"
from "URIGHTS" as right_2
join "URIGHTS" as right_5 using ("ID")
join "URIGHTS" as right_10 using ("ID")
where right_2."RIGHT" = 2and right_5."RIGHT" = 5and right_10."RIGHT" = 10
 or

select "ID"
from (select "ID"from "URIGHTS"where "RIGHT" = 2) as right_2
join (select "ID"from "URIGHTS"where "RIGHT" = 5) as right_5 using ("ID")
join (select "ID"from "URIGHTS"where "RIGHT" = 10) as right_10 using ("ID")


Simple is in the eye of the beholder. You might want to compare the  
EXPLAIN ANALYZE output to see if there are any significant  
differences between these queries.

Michael Glaesemann
grzm myrealbox com





pgsql-sql by date:

Previous
From: Ivan Steganov
Date:
Subject: Matching several rows
Next
From: "Christian Paul B. Cosinas"
Date:
Subject: Re: Characters that needs escape characters when inserting to database