Select whole team either by using parent_id or child_id : Mysql

641 views Asked by At

I have a table structure as fallows :

 id fnane parentid
  1 abc    null
  2 xyz    1
  3 mno    1

And need query all the team members by passing either parent id or child id. I have tried the fallowing query

SELECT 
    child.emailid, child.id
FROM
    user parent
        RIGHT JOIN
    user child ON child.parentid = parent.id
WHERE
    parent.parentid IS NULL
        AND (parent.id = '452fa614-572e-4b79-bb3c-71eccc5b5547'
        OR child.parentid = '452fa614-572e-4b79-bb3c-71eccc5b5547')
;

But not working in either of the fallowing cases.

case 1 : If i pass parentid fetching only child rows.

case 2: If i pass id(child record) 0 rows fetching.

And tried these links : Link 1 -- Link 2

3

There are 3 answers

1
Gurwinder Singh On
SELECT 
    child.emailid, child.id
FROM
    user parent
        RIGHT JOIN
    user child ON child.parentid = parent.id
AND  --and instead of where
    parent.parentid IS NULL
        AND (parent.id = '452fa614-572e-4b79-bb3c-71eccc5b5547'
        OR child.parentid = '452fa614-572e-4b79-bb3c-71eccc5b5547')
;
0
Kiran Kumar On

Try the below query,

SELECT 
child.emailid, child.id
FROM
user parent INNER JOIN
user child ON child.parentid = parent.id
WHERE
parent.parentid IS NULL
    AND (parent.id = '452fa614-572e-4b79-bb3c-71eccc5b5547'
    OR child.parentid = '452fa614-572e-4b79-bb3c-71eccc5b5547');
0
Pankaj katiyar On

try it with entire parent-child relationship

Table having parent child data

CREATE TABLE `categories` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `parent_id` int(10) NOT NULL DEFAULT '0',
  `name` varchar(255) NOT NULL,
  `color` varchar(10) DEFAULT '#efefef',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1106 DEFAULT CHARSET=latin1;



/*Data for the table `categories` */



insert  into `categories`(`id`,`parent_id`,`name`,`color`,`created_at`,`updated_at`) values (1,0,'php 5.5','#60c6ce','2016-06-06 13:54:41','2015-10-21 22:17:54'),(2,0,'Beauty','#f29cb2','2016-05-30 18:23:07','2015-10-16 02:24:31'),(3,0,'Travel','#26a8e0','2015-10-21 19:30:52','2015-10-21 22:30:52'),(4,0,'Food','#f9ba3d','2015-08-11 21:34:38','2015-08-11 21:34:38'),(5,0,'Wellness','#86c87b','2015-10-21 20:08:33','2015-10-21 23:08:33'),(6,0,'Inspiration','#97249a','2016-05-30 18:23:53','2015-10-15 20:09:47'),(7,0,'Culture','#ff6c18','2015-08-11 21:36:13','2015-08-11 21:36:13'),(1003,0,'Living','#8e9dcd','2015-10-21 20:04:21','2015-10-21 23:04:21'),(1006,1,'Cloth','#60c6ce','2016-06-01 16:25:25','2015-10-21 22:21:15'),(1009,1,'Shoes','#60c6ce','2016-06-01 16:25:40','2015-10-21 22:22:38'),(1011,1,'Bagssss','#60c6ce','2016-06-01 16:20:53','2015-10-21 22:22:02'),(1012,1,'Accessories','#60c6ce','2015-10-21 19:20:56','2015-10-21 22:20:56'),(1013,1,'Designers','#60c6ce','2015-10-21 19:22:21','2015-10-21 22:22:21'),(1014,2,'Make Up','#f29cb2','2015-10-15 20:20:15','2015-10-15 20:20:15'),(1015,2,'Skin','#f29cb2','2015-10-15 20:20:41','2015-10-15 20:20:41'),(1020,2,'Make Up','#f29bb2','2016-05-30 18:21:53','2015-10-21 22:26:23'),(1021,2,'Skin Care','#f29bb2','2016-05-30 18:21:54','2015-10-21 22:45:35'),(1022,2,'Hair','#f29bb2','2016-05-30 18:21:55','2015-10-21 22:28:40'),(1023,2,'Nails','#f29bb2','2016-05-30 18:21:57','2015-10-21 22:29:03'),(1024,2,'Fragrance','#f29bb2','2016-05-30 18:21:58','2015-10-21 22:29:40'),(1025,3,'Things To Do','#26a8e0','2015-10-21 22:31:26','2015-10-21 22:31:26'),(1026,3,'Hotels','#26a8e0','2015-10-21 22:31:54','2015-10-21 22:31:54'),(1027,3,'Restaurants','#26a8e0','2015-10-21 22:32:21','2015-10-21 22:32:21'),(1028,3,'City Travel Guides','#26a8e0','2015-10-21 22:33:02','2015-10-21 22:33:02'),(1029,3,'Local - New York','#26a8e0','2016-02-03 02:34:47','2016-02-03 04:34:47'),(1030,3,'Local - London','#26a8e0','2016-02-03 02:34:10','2016-02-03 04:34:10'),(1031,3,'Local - Paris','#26a8e0','2016-02-03 02:34:30','2016-02-03 04:34:30'),(1032,7,'Movies','#ff6c18','2015-10-21 22:59:15','2015-10-21 22:59:15'),(1033,7,'Music','#ff6c18','2015-10-21 23:00:05','2015-10-21 23:00:05'),(1034,7,'TV','#ff6c18','2015-10-21 23:00:53','2015-10-21 23:00:53'),(1035,7,'Books','#ff6c18','2015-10-21 23:01:09','2015-10-21 23:01:09'),(1036,7,'Theatre','#ff6c18','2015-10-21 23:01:35','2015-10-21 23:01:35'),(1037,7,'Shows','#ff6c18','2015-10-21 23:02:11','2015-10-21 23:02:11'),(1038,7,'Art','#ff6c18','2015-10-21 23:02:28','2015-10-21 23:02:28'),(1039,7,'Events','#ff6c18','2015-10-21 23:02:44','2015-10-21 23:02:44'),(1040,1,'Celebrities','#60c6ce','2015-10-21 23:03:33','2015-10-21 23:03:33'),(1041,1003,'Home','#8e9dcd','2015-10-21 23:04:40','2015-10-21 23:04:40'),(1042,1003,'Decor','#8e9dcd','2015-10-21 23:05:28','2015-10-21 23:05:28'),(1043,1003,'Entertaining','#8e9dcd','2015-10-21 23:06:06','2015-10-21 23:06:06'),(1044,1003,'DIY','#8e9dcd','2015-10-21 23:06:38','2015-10-21 23:06:38'),(1045,1003,'Tech','#8e9dcd','2015-10-21 23:06:54','2015-10-21 23:06:54'),(1046,1003,'Careers','#8e9dcd','2015-10-21 23:07:15','2015-10-21 23:07:15'),(1047,1003,'Finance','#8e9dcd','2015-10-21 23:07:31','2015-10-21 23:07:31'),(1048,5,'Living Well','#86c87b','2015-10-21 23:08:56','2015-10-21 23:08:56'),(1050,5,'Health','#86c87b','2015-10-21 23:15:16','2015-10-21 23:15:16'),(1051,5,'Spirit','#86c87b','2015-10-21 23:16:14','2015-10-21 23:16:14'),(1052,5,'Fitness','#86c87b','2015-10-21 23:16:39','2015-10-21 23:16:39'),(1053,5,'Relationships','#86c87b','2015-10-21 23:17:02','2015-10-21 23:17:02'),(1054,5,'Parenting','#86c87b','2015-10-21 23:17:24','2015-10-21 23:17:24'),(1055,5,'Diet & Nutrition','#86c87b','2015-10-21 23:18:04','2015-10-21 23:18:04'),(1056,4,'Restaurants','#f9ba3d','2015-10-21 23:19:03','2015-10-21 23:19:03'),(1057,4,'Recipes','#f9ba3d','2015-10-21 23:19:28','2015-10-21 23:19:28'),(1058,4,'Groceries','#f9ba3d','2015-10-21 23:19:47','2015-10-21 23:19:47'),(1059,4,'Drinks','#f9ba3d','2015-10-21 23:20:03','2015-10-21 23:20:03'),(1060,4,'Healthy','#f9ba3d','2015-10-21 23:20:21','2015-10-21 23:20:21'),(1061,4,'Gourmet','#f9ba3d','2015-10-21 23:20:39','2015-10-21 23:20:39'),(1062,4,'Brunch','#f9ba3d','2015-10-21 23:20:54','2015-10-21 23:20:54'),(1063,4,'Desserts','#f9ba3d','2015-10-21 23:21:15','2015-10-21 23:21:15'),(1064,0,'Inspire','#5c3c77','2015-10-21 23:21:59','2015-10-21 23:21:59'),(1065,1064,'Great Women','#5c3c77','2015-10-21 23:22:39','2015-10-21 23:22:39'),(1066,1064,'Influencers','#5c3c77','2015-10-21 23:23:47','2015-10-21 23:23:47'),(1067,1064,'Other','#5c3c77','2015-10-21 23:24:07','2015-10-21 23:24:07'),(1068,7,'Musicals','#ff6c18','2015-10-30 03:26:57','2015-10-30 03:26:57'),(1069,1064,'Blogs','#5c3c77','2015-10-30 03:55:52','2015-10-30 03:55:52'),(1070,1064,'Quotes','#5c3c77','2015-10-30 03:57:51','2015-10-30 03:57:51'),(1071,1,'Tops','#60c6ce','2015-10-30 04:07:23','2015-10-30 04:07:23'),(1072,1,'Bottoms','#60c6ce','2015-10-30 04:07:39','2015-10-30 04:07:39'),(1073,1,'Dresses','#60c6ce','2015-10-30 04:07:58','2015-10-30 04:07:58'),(1074,1,'Jackets','#60c6ce','2015-10-30 04:08:21','2015-10-30 04:08:21'),(1075,1,'Jewelry','#60c6ce','2015-10-30 04:08:41','2015-10-30 04:08:41'),(1077,1,'Trends','#60c6ce','2015-10-30 04:09:29','2015-10-30 04:09:29'),(1078,1,'Personal Style','#60c6ce','2015-10-30 04:10:06','2015-10-30 04:10:06'),(1079,1003,'Celebrations','#8e9dcd','2015-10-30 04:17:37','2015-10-30 04:17:37'),(1080,1003,'Gifts Ideas','#8e9dcd','2015-10-30 04:18:07','2015-10-30 04:18:07'),(1081,3,'Vacation Ideas','#26a8e0','2015-10-30 04:27:34','2015-10-30 04:27:34'),(1082,1019,'Bath & Body','#f29bb2','2015-11-01 03:56:29','2015-11-01 03:56:29'),(1083,1019,'Tools & Brushes','#f29bb2','2015-11-01 03:56:51','2015-11-01 03:56:51'),(1084,1064,'Careers','#5c3c77','2015-12-29 23:06:04','2015-12-29 23:06:04'),(1085,1064,'Women Entrepreneurs','#5c3c77','2015-12-29 23:06:57','2015-12-29 23:06:57'),(1086,1,'Activewear','#60c6ce','2016-01-29 00:28:36','2016-01-29 00:28:36'),(1087,1064,'Articles','#5c3c77','2016-01-29 00:29:39','2016-01-29 00:29:39'),(1088,1003,'Kids','#8e9dcd','2016-01-29 00:30:28','2016-01-29 00:30:28'),(1089,5,'Relaxing','#86c87b','2016-01-29 00:38:04','2016-01-29 00:38:04'),(1090,1019,'Skin Treatments','#f29bb2','2016-02-02 18:45:30','2016-02-02 18:45:30'),(1092,3,'Local - Bangkok','#26a8e0','2016-02-03 02:33:51','2016-02-03 04:33:51'),(1093,7,'Museums','#ff6c18','2016-02-03 23:27:04','2016-02-03 23:27:04'),(1094,7,'Galleries','#ff6c18','2016-02-03 23:28:10','2016-02-03 23:28:10'),(1095,4,'Bakeries','#f9ba3d','2016-02-08 23:24:54','2016-02-08 23:24:54'),(1096,1,'test','#efefef','2016-06-01 15:51:25','0000-00-00 00:00:00'),(1100,0,'parent','#efefef','2016-06-01 16:49:12','0000-00-00 00:00:00'),(1101,2,'pankaj katiyar','#efefef','2016-06-03 16:37:13','0000-00-00 00:00:00'),(1103,5,'ght','#efefef','2016-06-03 11:47:12','0000-00-00 00:00:00'),(1104,2,'new categ','#efefef','2016-06-03 11:50:10','0000-00-00 00:00:00'),(1105,0,'Root','#efefef','2016-06-08 16:51:45','0000-00-00 00:00:00');

Create function for get child data

DELIMITER $$

USE `your_database`$$

DROP FUNCTION IF EXISTS `GetAllNode`$$

CREATE DEFINER=`root`@`localhost` FUNCTION `GetAllNode`(GivenID INT) RETURNS TEXT CHARSET latin1
    DETERMINISTIC
BEGIN
    DECLARE rv,q,queue,queue_children TEXT;
    DECLARE queue_length,front_id,pos INT;
    SET rv = '';
    SET queue = GivenID;
    SET queue_length = 1;
    WHILE queue_length > 0 DO
        SET front_id = queue;
        IF queue_length = 1 THEN
            SET queue = '';
        ELSE
            SET pos = LOCATE(',',queue) + 1;
            SET q = SUBSTR(queue,pos);
            SET queue = q;
        END IF;
        SET queue_length = queue_length - 1;
        SELECT IFNULL(qc,'') INTO queue_children
        FROM (SELECT GROUP_CONCAT(id) AS qc
        FROM `categories` WHERE `parent_id` = front_id) A ;
        IF LENGTH(queue_children) = 0 THEN
            IF LENGTH(queue) = 0 THEN
                SET queue_length = 0;
            END IF;
        ELSE
            IF LENGTH(rv) = 0 THEN
                SET rv = queue_children;
            ELSE
                SET rv = CONCAT(rv,',',queue_children);
            END IF;
            IF LENGTH(queue) = 0 THEN
                SET queue = queue_children;
            ELSE
                SET queue = CONCAT(queue,',',queue_children);
            END IF;
            SET queue_length = LENGTH(queue) - LENGTH(REPLACE(queue,',','')) + 1;
        END IF;
    END WHILE;
    RETURN rv;
END$$

DELIMITER ;

get data for desire result passing primary key

SELECT GetAllNode(id)  AS ids FROM `categories`  WHERE id=2

result(cancat child ids) look like
1014,1015,1020,1021,1022,1023,1024,1101,1104