Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Interesting query

Status
Not open for further replies.

stasJohn

Programmer
May 6, 2004
155
US
Hi, I'm curious if the follwoing can be done.

First, here's sample table data

Code:
| id | parent | ordering |
| 1  | 0      | 1        |
| 2  | 0      | 2        |
| 3  | 0      | 3        |
| 4  | 2      | 1        |
| 5  | 2      | 2        |

I want to perform a query which returns the ids like;
Code:
1
2
4
5
3


I'm using mysql 4.1

Any hints would be a great help. thanks in Advance.
 
The table is used to store a hierarchical menu structure.

For example:
Code:
Home
About
  About1
Tips
  Tips1
  Tips2

Code:
id | name      | parent | ordering |
------------------------------------
1  | Home      | 0      | 1        |
2  | About     | 0      | 2        |
3  | Tips      | 0      | 3        |
4  | About1    | 2      | 1        |
5  | Tips2     | 3      | 2        |
6  | Tips1     | 3      | 1        |

I want to query the db to return all menu items and have them listed in the order the menu items should be listed; Home, About, About1, Tips, Tips1, Tips2

I hope that makes better sense.


(I realize there may be a better way to set up the table, but this is the structure I'm stuck with)
 
this will work exactly as requested on the sample provided --
Code:
order by
 case when parent=0 then id else parent end
,case when parent=0 then 0 else 1 end
,ordering
note that if your nav goes more than one level deep, it won't

:)

r937.com | rudy.ca
 
Thanks r937 for the code.

The code works great when the "ids" are in order, but not if out of order. For instance, lets say "About" is deleted and the re-added;

Code:
id | name      | parent | ordering |
------------------------------------
1  | Home      | 0      | 1        |
3  | Tips      | 0      | 3        |
5  | Tips2     | 3      | 2        |
6  | Tips1     | 3      | 1        |
7  | About     | 0      | 2        |
8  | About1    | 7      | 1        |

The results from the sql will be
Code:
Home
Tips
  Tips1
  Tips2
About
  About1

"About" should actually preceed Tips
 
okay, try this --
Code:
select t1.id
     , t1.parent
     , t1.ordering
     , t1.name
     , t1.ordering as sortkey1
     , 0           as sortkey2
  from stasJohn as t1
 where t1.parent = 0  
union all
select t2.id
     , t2.parent
     , t2.ordering
     , t2.name
     , t1.ordering as sortkey1
     , t2.ordering as sortkey2
  from stasJohn as t1
inner
  join stasJohn as t2
    on t2.parent = t1.id
 where t1.parent = 0  
order
    by sortkey1
     , sortkey2
results:
Code:
id parent ordering name sortkey1 sortkey2
1  0  1  Home    1  0
7  0  2  About   2  0
8  7  1  About1  2  1
3  0  3  Tips    3  0
6  3  1  Tips1   3  1
5  3  2  Tips2   3  2

r937.com | rudy.ca
 
Glad this topic came up.... Is there a single query that be done for a table designed like a linked list, where ordering is dictated only by the parent:

Code:
id | name      | parent |
-------------------------
1  | First     |   0    |
2  | Second    |   1    |
3  | Fourth    |   5    |
4  | Fifth     |   3    |
5  | Third     |   2    |
6  | Sixth     |   4    |

Thanks - Jim
 
But I take it there is no single query that will do this for lists of variable size?

Thanks - Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top