![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I'm not sure how to even ask about this, nor how to phrase this for Google, but here it goes. I have a table, category, eg: CREATE TABLE category (id INT AUTO_INCREMENT, category VARCHAR(20), children VARCHAR(2), parent INT, PRIMARY KEY(id)); INSERT INTO category (category, children, parent) VALUES ('Bakery','Y',NULL), ('Cakes',NULL,1), ('Pies',NULL,1), ('Auto','Y',NULL), ('Repair',NULL,4), ('Sales',NULL,4); What I want to see is something like: Bakery Bakery - Cakes Bakery - Pies Auto Auto - Repair Auto - Sales What I am doing now is: SELECT id, category FROM category WHERE childen = 'Y' and as I loop through the result set, I am doing yet another query: SELECT id, category FROM category WHERE parent = "parentid" It seems there must be a better way to do this. TIA for any help or ideas. |
|
category | child_category | +----------+----------------+ Auto | Repair | Auto | Sales | Bakery | Cakes | Bakery | Pies | +----------+----------------+ |
|
category | child_category | +----------+----------------+ Auto | | Auto | Repair | Auto | Sales | Bakery | | Bakery | Cakes | Bakery | Pies | +----------+----------------+ |
#3
| |||
| |||
|
|
I'm not sure how to even ask about this, nor how to phrase this for Google, but here it goes. I have a table, category, eg: CREATE TABLE category (id INT AUTO_INCREMENT, category VARCHAR(20), children VARCHAR(2), parent INT, PRIMARY KEY(id)); INSERT INTO category (category, children, parent) VALUES ('Bakery','Y',NULL), ('Cakes',NULL,1), ('Pies',NULL,1), ('Auto','Y',NULL), ('Repair',NULL,4), ('Sales',NULL,4); What I want to see is something like: Bakery Bakery - Cakes Bakery - Pies Auto Auto - Repair Auto - Sales What I am doing now is: SELECT id, category FROM category WHERE childen = 'Y' and as I loop through the result set, I am doing yet another query: SELECT id, category FROM category WHERE parent = "parentid" It seems there must be a better way to do this. *TIA for any help or ideas. -- Adrienne Boswell at Home Arbpen Web Site Design Serviceshttp://www.cavalcade-of-coding.info Please respond to the group so others can share |
#4
| |||
| |||
|
|
Hi Adrienne, take a look at the links here: http://www.delicious.com/Captain_Paralytic/hierarchical |
#5
| |||
| |||
|
|
On 2011-03-08 06:46, Adrienne Boswell wrote: I'm not sure how to even ask about this, nor how to phrase this for Google, but here it goes. I have a table, category, eg: CREATE TABLE category (id INT AUTO_INCREMENT, category VARCHAR(20), children VARCHAR(2), parent INT, PRIMARY KEY(id)); INSERT INTO category (category, children, parent) VALUES ('Bakery','Y',NULL), ('Cakes',NULL,1), ('Pies',NULL,1), ('Auto','Y',NULL), ('Repair',NULL,4), ('Sales',NULL,4); What I want to see is something like: Bakery Bakery - Cakes Bakery - Pies Auto Auto - Repair Auto - Sales What I am doing now is: SELECT id, category FROM category WHERE childen = 'Y' and as I loop through the result set, I am doing yet another query: SELECT id, category FROM category WHERE parent = "parentid" It seems there must be a better way to do this. TIA for any help or ideas. I assume Bakery the rows with one column is some kind of header, and that should IMO be part of presentation, not logic. select x.category, y.category as child_category from category x join category y on y.parent = x.id order by category, child_category; +----------+----------------+ | category | child_category | +----------+----------------+ | Auto | Repair | | Auto | Sales | | Bakery | Cakes | | Bakery | Pies | +----------+----------------+ 4 rows in set (0.00 sec) As you can see the children column is redundant and can be removed. If you keep it and try to maintain it you face the risk of introducing anomalies. If you insist on retriveing the header rows it can be done something like: select x.category, y.category as child_category from category x join category y on y.parent = x.id union select category, '' from category x where not exists ( select 1 from category y where x.parent = y.id ) order by category, child_category; +----------+----------------+ | category | child_category | +----------+----------------+ | Auto | | | Auto | Repair | | Auto | Sales | | Bakery | | | Bakery | Cakes | | Bakery | Pies | +----------+----------------+ 6 rows in set (0.00 sec) /Lennart |
![]() |
| Thread Tools | |
| Display Modes | |
| |