• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

Join table on self

I am trying to select all rows in a table where the parent id is the passed value, but I also want to select all rows whose parent id equals the above selected rows ID

Example structure
tbl_categories_id    category_name    category_parent
1                                 cat1                                 1
2                                 cat2                                 1
3                                 cat3                                 1
4                                 cat4                                 2
5                                 cat5                                 2

So select all rows where the category_parent=1, but of those there is one (tbl_categories_id=2) which has 2 child rows (4 & 5)

How would I write the sql?

http://sqlfiddle.com/#!2/097e0/2
0
Gary
Asked:
Gary
  • 3
  • 2
1 Solution
 
SharathData EngineerCommented:
So if you pass category_parent as 1, you want all its children and their children in a recursive way?
0
 
GaryAuthor Commented:
Yep. So even though I am selecting where parent_id=1 my query should return

1                                 cat1                                 1
2                                 cat2                                 1
     4                                 cat4                                 2
     5                                 cat5                                 2
3                                 cat3                                 1
0
 
SharathData EngineerCommented:
check this.
    SET NAMES utf8;
    SET foreign_key_checks = 0;
    SET time_zone = '+01:00';
    SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
    
    DROP TABLE IF EXISTS `categories`;
    CREATE TABLE `categories` (
      `tbl_categories_id` int(11) NOT NULL AUTO_INCREMENT,
      `category_name` varchar(100) DEFAULT NULL,
      `category_parent` int(2) DEFAULT '0',
    
      UNIQUE KEY `tbl_categories_id` (`tbl_categories_id`)
    
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    
    
    -- 2013-10-17 22:40:09
    
    INSERT INTO `categories` (`tbl_categories_id`, `category_name`, `category_parent`) VALUES
    (1,	'cat1',	0),
    (2,	'cat2',	1),
    (3,	'cat3',	1),
    (4,	'cat4',	1),
    (5,	'cat5',	2),
    (6,	'cat6',	3)

**Query 1**:

    SELECT * 
      FROM categories 
     WHERE category_parent = 1 
        OR category_parent IN (SELECT tbl_categories_id 
                                 FROM (SELECT @r := (SELECT MIN(tbl_categories_id) 
                                                       FROM categories 
                                                      WHERE category_parent = @r) AS tbl_categories_id
                                         FROM (SELECT @r := 1) vars, 
                                              categories) t1) 

**[Results][2]**:
    
    | TBL_CATEGORIES_ID | CATEGORY_NAME | CATEGORY_PARENT |
    |-------------------|---------------|-----------------|
    |                 2 |          cat2 |               1 |
    |                 3 |          cat3 |               1 |
    |                 4 |          cat4 |               1 |
    |                 5 |          cat5 |               2 |

Open in new window

http://sqlfiddle.com/#!2/097e0/12
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
GaryAuthor Commented:
Will get back to this tomorrow, got into the middle of something else and don't want to get sidetracked.
0
 
SharathData EngineerCommented:
Thanks Paul for posting the sqlfiddle from my post. will post the solution going forward.
0
 
PortletPaulfreelancerCommented:
:) no problem Sharath. Cheers, Paul.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now