开发者

Tricky MySQL ORDER BY problem

I have the following table:

id  title               parent_forum    depth
1   'Forum1'             0               0   
2   'Forum2'             0               0   
3   'Forum3'开发者_如何学Python             0               0   
4   'Forum1 subforum'    1               0   
5   'Forum2 subForum'    2               0   
6   'Forum3 subForum'    3               0

What I want to do is sort the rows so that all subForums are grouped by their parent forums. So, as an example, I would like the output to be ordered as follows:

id  title               parent_forum    depth
1   'Forum1'             0               0
4   'Forum1 subforum'    1               0

2   'Forum2'             0               0  
5   'Forum2 subForum'    2               0
7   'Forum2 subForum2'   2               0

3   'Forum3'             0               0   
6   'Forum3 subForum'    3               0

(spaces for better understanding) There must be a way with just ORDER BY and GROUP BY? If not, can anyone offer a good query to help me out?

EDIT: Or does my database structure just suck (I haven't shown the whole lot, but is the stuff you CAN see rubbish?)

Thanks muchly,

James


I think this aught to do the trick:

ORDER BY IF(parent_forum = 0,id,parent_forum), parent_forum
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜