Advertisement

05.08.2008 at 08:08AM PDT, ID: 23386348
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.3

UNION or JOIN with sub queries

Asked by josh2780 in MySQL Server

Tags:

I have hierarchical data stored in a table and I'm trying to write a query that will return the full path to an item in one result.  For example, assume the following table and data:

CREATE TABLE treedata (id int,title varchar(25),lft int,rgt int,primary key (id));
INSERT INTO treedata VALUES (1,'Food',1,14);
INSERT INTO treedata VALUES (2,'Fruit',2,9);
INSERT INTO treedata VALUES (3,'Red',3,6);
INSERT INTO treedata VALUES (4,'Cherry',4,5);
INSERT INTO treedata VALUES (5,'Yellow',7,8);
INSERT INTO treedata VALUES (6,'Meat',10,13);
INSERT INTO treedata VALUES (7,'Beef',11,12);

For 'Cherry', the result of my query should return 'Food,Fruit,Red,Cherry'.  I can accomplish this with the following query:
SELECT GROUP_CONCAT(td.title) as fullName FROM (SELECT title FROM treedata WHERE lft <= 4 and rgt >= 5 ORDER BY lft ASC) td;

What I'd like to do is modify this query to return all of the results in the tree (without having to supply a lft and rgt value as I did in the query above).  For instance, the modified query would return a result set similar to:
Food
Food,Fruit
Food,Fruit,Red
Food,Fruit,Red,Cherry
Food,Fruit,Yellow
... and so on ...

I've tried with various JOINs and UNIONs but have been unsuccessful.  Thanks for your help.Start Free Trial
 
Loading Advertisement...
 
[+][-]05.08.2008 at 08:27AM PDT, ID: 21525551

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: MySQL Server
Tags: mysql
Sign Up Now!
Solution Provided By: Thomasian
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628