dbTalk Databases Forums  

Single query to find multi-level relationships?

comp.databases comp.databases


Discuss Single query to find multi-level relationships? in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
John Gordon
 
Posts: n/a

Default Single query to find multi-level relationships? - 05-28-2008 , 12:05 PM






I'm writing some code to suport a contact database, where a contact
can be either a person or a group. Groups can contain people directly
and can also contain other groups.

The (simplified) table structure looks like this:

PERSON
------
person_id number primary key

GROUP
-----
group_id number primary key

MEMBER_OF_GROUP
---------------
person_id number primary key 1 (foreign key -> PERSON.person_id)
group_id number primary key 2 (foreign key -> GROUP.group_id)

GROUP_IN_GROUP
--------------
subgroup_id number primary key 1 (foreign key -> GROUP.group_id)
supergroup_id number primary key 2 (foreign key -> GROUP.group_id)

I want to write a single query that will tell me if a person is a member
of a given group, whether as a direct member via the MEMBER_OF_GROUP
table or as an indirect member of a child group via the GROUP_IN_GROUP
table.

Anyone have suggestions?

--
John Gordon A is for Amy, who fell down the stairs
gordon (AT) panix (DOT) com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"


Reply With Quote
  #2  
Old   
David Cressey
 
Posts: n/a

Default Re: Single query to find multi-level relationships? - 05-29-2008 , 05:05 AM







"John Gordon" <gordon (AT) panix (DOT) com> wrote

Quote:
I'm writing some code to suport a contact database, where a contact
can be either a person or a group. Groups can contain people directly
and can also contain other groups.

The (simplified) table structure looks like this:

PERSON
------
person_id number primary key

GROUP
-----
group_id number primary key

MEMBER_OF_GROUP
---------------
person_id number primary key 1 (foreign key -> PERSON.person_id)
group_id number primary key 2 (foreign key -> GROUP.group_id)

GROUP_IN_GROUP
--------------
subgroup_id number primary key 1 (foreign key -> GROUP.group_id)
supergroup_id number primary key 2 (foreign key -> GROUP.group_id)

I want to write a single query that will tell me if a person is a member
of a given group, whether as a direct member via the MEMBER_OF_GROUP
table or as an indirect member of a child group via the GROUP_IN_GROUP
table.

Anyone have suggestions?

--
John Gordon A is for Amy, who fell down the stairs
gordon (AT) panix (DOT) com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Take a look at the nested set model. With nested sets, you'll be able to
query for indirect children without coding up a recursive query.





Reply With Quote
  #3  
Old   
David Cressey
 
Posts: n/a

Default Re: Single query to find multi-level relationships? - 05-29-2008 , 05:05 AM




"John Gordon" <gordon (AT) panix (DOT) com> wrote

Quote:
I'm writing some code to suport a contact database, where a contact
can be either a person or a group. Groups can contain people directly
and can also contain other groups.

The (simplified) table structure looks like this:

PERSON
------
person_id number primary key

GROUP
-----
group_id number primary key

MEMBER_OF_GROUP
---------------
person_id number primary key 1 (foreign key -> PERSON.person_id)
group_id number primary key 2 (foreign key -> GROUP.group_id)

GROUP_IN_GROUP
--------------
subgroup_id number primary key 1 (foreign key -> GROUP.group_id)
supergroup_id number primary key 2 (foreign key -> GROUP.group_id)

I want to write a single query that will tell me if a person is a member
of a given group, whether as a direct member via the MEMBER_OF_GROUP
table or as an indirect member of a child group via the GROUP_IN_GROUP
table.

Anyone have suggestions?

--
John Gordon A is for Amy, who fell down the stairs
gordon (AT) panix (DOT) com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Take a look at the nested set model. With nested sets, you'll be able to
query for indirect children without coding up a recursive query.





Reply With Quote
  #4  
Old   
David Cressey
 
Posts: n/a

Default Re: Single query to find multi-level relationships? - 05-29-2008 , 05:05 AM




"John Gordon" <gordon (AT) panix (DOT) com> wrote

Quote:
I'm writing some code to suport a contact database, where a contact
can be either a person or a group. Groups can contain people directly
and can also contain other groups.

The (simplified) table structure looks like this:

PERSON
------
person_id number primary key

GROUP
-----
group_id number primary key

MEMBER_OF_GROUP
---------------
person_id number primary key 1 (foreign key -> PERSON.person_id)
group_id number primary key 2 (foreign key -> GROUP.group_id)

GROUP_IN_GROUP
--------------
subgroup_id number primary key 1 (foreign key -> GROUP.group_id)
supergroup_id number primary key 2 (foreign key -> GROUP.group_id)

I want to write a single query that will tell me if a person is a member
of a given group, whether as a direct member via the MEMBER_OF_GROUP
table or as an indirect member of a child group via the GROUP_IN_GROUP
table.

Anyone have suggestions?

--
John Gordon A is for Amy, who fell down the stairs
gordon (AT) panix (DOT) com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Take a look at the nested set model. With nested sets, you'll be able to
query for indirect children without coding up a recursive query.





Reply With Quote
  #5  
Old   
--CELKO--
 
Posts: n/a

Default Re: Single query to find multi-level relationships? - 05-29-2008 , 06:57 AM



get a copy of TREES & HIERARCHIES IN SQL and look at the nested sets
model.

Reply With Quote
  #6  
Old   
--CELKO--
 
Posts: n/a

Default Re: Single query to find multi-level relationships? - 05-29-2008 , 06:57 AM



get a copy of TREES & HIERARCHIES IN SQL and look at the nested sets
model.

Reply With Quote
  #7  
Old   
--CELKO--
 
Posts: n/a

Default Re: Single query to find multi-level relationships? - 05-29-2008 , 06:57 AM



get a copy of TREES & HIERARCHIES IN SQL and look at the nested sets
model.

Reply With Quote
  #8  
Old   
John Gordon
 
Posts: n/a

Default Re: Single query to find multi-level relationships? - 06-02-2008 , 01:20 PM



In <2ev%j.3602$GT5.3283@trndny01> "David Cressey" <cressey73 (AT) verizon (DOT) net> writes:

Quote:
Take a look at the nested set model. With nested sets, you'll be able to
query for indirect children without coding up a recursive query.
Unfortunately I don't have the option of modifying the table structure
to represent the data as a nested set. I must work with the data as
given.

Is there a way to do this in standard SQL?

--
John Gordon A is for Amy, who fell down the stairs
gordon (AT) panix (DOT) com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"



Reply With Quote
  #9  
Old   
John Gordon
 
Posts: n/a

Default Re: Single query to find multi-level relationships? - 06-02-2008 , 01:20 PM



In <2ev%j.3602$GT5.3283@trndny01> "David Cressey" <cressey73 (AT) verizon (DOT) net> writes:

Quote:
Take a look at the nested set model. With nested sets, you'll be able to
query for indirect children without coding up a recursive query.
Unfortunately I don't have the option of modifying the table structure
to represent the data as a nested set. I must work with the data as
given.

Is there a way to do this in standard SQL?

--
John Gordon A is for Amy, who fell down the stairs
gordon (AT) panix (DOT) com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"



Reply With Quote
  #10  
Old   
John Gordon
 
Posts: n/a

Default Re: Single query to find multi-level relationships? - 06-02-2008 , 01:20 PM



In <2ev%j.3602$GT5.3283@trndny01> "David Cressey" <cressey73 (AT) verizon (DOT) net> writes:

Quote:
Take a look at the nested set model. With nested sets, you'll be able to
query for indirect children without coding up a recursive query.
Unfortunately I don't have the option of modifying the table structure
to represent the data as a nested set. I must work with the data as
given.

Is there a way to do this in standard SQL?

--
John Gordon A is for Amy, who fell down the stairs
gordon (AT) panix (DOT) com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.