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!

Arranging query output with multiple ORDER BY 1

Status
Not open for further replies.

JRSofty

Technical User
Jul 7, 2004
12
DE
Here is the table I have

uid, pid, dis_order, title

uid = Unique ID
pid = Parent ID
dis_order = Display Order
title = title

Ok so I need to display this table in such a way that
1. ordered by display order
2. children are under their parents
3. children are also in their display order

I have this SQL that will get the children under their parents

Code:
SELECT * FROM `mytable` ORDER BY IF(`pid` = 0, `id`*100, `pid`*100+`id`)

but I can't seem to get everyone (including the parents) into display order.

Anyone got an idea?

JRSofty
 
Ok that works like my IF statement but it doesn't get them into dis_order (display order) as well unless they all have sequential ids. In this case they might not have sequential ids where id=1 the dis_order might be 2 so that id=1 need be the second in the list.





JRSofty
 
sorry, you're right, i forgot about the dis_order
Code:
select c.uid 
     , c.pid
     , c.dis_order
     , c.title
  from mytable as c
left outer
  join mytable as p
    on c.pic = p.uid
order
    by coalesce(p.dis_order,c.dis_order)
     , c.dis_order

rudy | r937.com | Ask the Expert | Premium SQL Articles
SQL for Database-Driven Web Sites (next course starts May 8 2005)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top