Jump to content

Recommended Posts

Hi all,

 

I am still pretty green when it comes to MYSQL. I have a categories table that looks like this:

CREATE TABLE IF NOT EXISTS `categories` (
  `category_id` int(10) unsigned NOT NULL auto_increment,
  `category_name` varchar(45) NOT NULL,
  `parent_id` int(10) unsigned default NULL,
  `category_type_id` int(10) unsigned NOT NULL,
  PRIMARY KEY  (`category_id`),
  KEY `idx_parent_id` (`parent_id`),
  KEY `idx_name` (`category_name`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=217 ;

 

We store Parent categories in the same table as the child categories. parent categories have a parent_id = 0. What I want to do is just pull all the categories and order them by parent_id but also have the parent categories grouped with the children.

 

so I need something like  'SELECT * FROM categories ORDER BY parent_id"

 

but taken further so that the parents are not all grouped together. I do not know if I need a subquery or what.

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/151073-same-table-order-by-help/
Share on other sites

To better explain my issue. Using what sdi126 recommend and what I have tried I am getting table rows returned in an order similar to:

 

parent1

parent2

parent3

parent4

child of parent 1

child of parent 1

child of parent 1

child of parent 1

child of parent 2

child of parent 2

child of parent 2

etc..

 

I am trying to get them returned like this:

 

Parent1

child of parent1

child of parent1

Parent2

child of parent2

child of parent2

Parent3

child of parent 3

child of parent 3

etc...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.