Subject | Re: [firebird-support] Self-referencing table SQL question |
---|---|
Author | Sam Hunt |
Post date | 2006-06-05T23:46:15Z |
diwic2 wrote:
ID* Date_Type* ParentID Name
1 1 <null> New World
2 1 <null> Old World
1 2 2 Sweden
2 2 2 Germany
3 2 2 Stockholm
4 2 1 America
(In the above, the parent recs and child recs were inserted in any order
but "ordered by" Data_Type,ID ASC.)
then
select Name where ParentID=2
would yield Sweden,Germany, and Stockholm
and
select Name where ParentID=1
would yield America.
The Data_Type column isn't required, but helps to conceptualize the data
relationships.
My 2 cents.
Sam D
> I have a self-referencing table, something like:You could define your tbl struct as: Data_Type 1 = Parent and 2 = child
>
> id integer
> name varchar(98)
> parentid integer (references another row in the same table)
>
> say I have these rows:
> ID ParentID Name
> 1 <null> World
> 2 1 Europe
> 3 2 Sweden
> 4 2 Germany
> 5 3 Stockholm
> 6 3 Gothenburg
> 7 4 Berlin
>
ID* Date_Type* ParentID Name
1 1 <null> New World
2 1 <null> Old World
1 2 2 Sweden
2 2 2 Germany
3 2 2 Stockholm
4 2 1 America
(In the above, the parent recs and child recs were inserted in any order
but "ordered by" Data_Type,ID ASC.)
then
select Name where ParentID=2
would yield Sweden,Germany, and Stockholm
and
select Name where ParentID=1
would yield America.
The Data_Type column isn't required, but helps to conceptualize the data
relationships.
My 2 cents.
Sam D