php - How do I get grand children or great-grand children of parent -


i have table below

create table member (     id varchar(11) primary key not null,     referral_id varchar(10) not null,     first_name varchar(100) not null,     surname varchar(100) not null,     address varchar(100) not null,     country varchar(20) not null,     phone varchar(20) not null,     account_no varchar(20) not null,     account_name varchar(100) not null,     bank_name varchar(50) not null,     email varchar(100) not null,     voucher_code varchar(20) not null,     vcode_activated varchar(2) not null,     current_level varchar(2) not null,     extra_downlines varchar(2) not null,     password varchar(100) not null,     status varchar(2) not null,     primary_downlines varchar(2) not null,     date_registered varchar(12) not null,     completed_level1 varchar(2) not null,     referral_bonus varchar(10) default '0',     signup_earning varchar(10) default '0' ); insert member (id, referral_id, first_name, surname, address, country, phone, account_no, account_name, bank_name, email, voucher_code, vcode_activated, completed_level1, current_level, primary_downlines, extra_downlines, password, status, date_registered, referral_bonus, signup_earning) values ('100000', '0', 'mose', 'alfi', 'mkd', 'na', '098', '987', 'alfi', 'gtb', 'al@yahc.com', '1504895239', '0', '1', '0', '', '1', 'a', '', '', '2000', '2000'); insert member (id, referral_id, first_name, surname, address, country, phone, account_no, account_name, bank_name, email, voucher_code, vcode_activated, completed_level1, current_level, primary_downlines, extra_downlines, password, status, date_registered, referral_bonus, signup_earning) values ('100001', '100000', 'ush', 'alim', 'mkd', 'ad', '0949490', '0987', 'ush', 'fbn', 'us@alim.com', '', '0', '', '0', '', '0', 'ali', '0', '', '0', '0'); insert member (id, referral_id, first_name, surname, address, country, phone, account_no, account_name, bank_name, email, voucher_code, vcode_activated, completed_level1, current_level, primary_downlines, extra_downlines, password, status, date_registered, referral_bonus, signup_earning) values ('100002', '100000', 'tork', 'alik', 'mkd', 'au', '0987', '098', 'tor', 'diamond', 'torku@alik.com', '', '0', '', '0', '', '0', 'ali', '0', '', '0', '0'); insert member (id, referral_id, first_name, surname, address, country, phone, account_no, account_name, bank_name, email, voucher_code, vcode_activated, completed_level1, current_level, primary_downlines, extra_downlines, password, status, date_registered, referral_bonus, signup_earning) values ('100003', '0', 'te', 'aliu', 'gboko', 'ad', '09809', '798', 'aliu ter', 'stanbic', 'ali@er.com', '', '0', '0', '1', '0', '0', 'ali', '0', '2017-09-11', '0', '0'); insert member (id, referral_id, first_name, surname, address, country, phone, account_no, account_name, bank_name, email, voucher_code, vcode_activated, completed_level1, current_level, primary_downlines, extra_downlines, password, status, date_registered, referral_bonus, signup_earning) values ('100004', '100000', 'alex', 'alis', 'mkd', 'ni', '9890', '99', 'alex', 'eco', 'alis@yshso.com', '', '0', '0', '1', '0', '0', 'alex', '0', '2017-09-11', '0', '0'); insert  member (id, referral_id, first_name, surname, address, country, phone, account_no, account_name, bank_name, email, voucher_code, vcode_activated, completed_level1, current_level, primary_downlines, extra_downlines, password, status, date_registered, referral_bonus, signup_earning) values ('100005', '100000', 'alx', 'alki', 'mkd', 'ni', '9890', '99', 'alx', 'eco', 'alki@yshso.com', '', '0', '0', '1', '0', '0', 'alex', '0', '2017-09-11', '0', '0'); 

in above member table, id member id, referral_id id of member referred member. member refer 3 people become members , each of them refer 3 people. how member has not referred 3 people? how hierarchical display of member , people has referred?

this expression returns children (including grand children) of main parent (id 100000) have less 3 own children.

select `id`, `referral_id` `parent_id`, `children` (select `id`, `referral_id`, (select count(`id`) `member` `r` r.`referral_id` = m.`id`) `children` `member` `m` order `referral_id`, `id`) `members`,      (select @pv := '100000') `init` find_in_set(`referral_id`, @pv) > 0 , @pv := concat(@pv, ',', `id`) having `children` < 3 

http://sqlfiddle.com/#!9/885878/4


if member has 3 or more children grand children excluded result too.

select `id`, `referral_id` `parent_id`, `children` (select `id`, `referral_id`, (select count(`id`) `member` `r` r.`referral_id` = m.`id`) `children` `member` `m` having `children` < 3 order `referral_id`, `id`) `members`,      (select @pv := '100000') `init` find_in_set(`referral_id`, @pv) > 0 , @pv := concat(@pv, ',', `id`) 

http://sqlfiddle.com/#!9/885878/5


in answer can find additional info on recursive queries. https://stackoverflow.com/a/33737203/4020014


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -