Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3725368
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T06:58:55+00:00 2026-05-19T06:58:55+00:00

I having a referral table like below. > id referredByID referrerID > > 1001

  • 0

I having a referral table like below.

> id     referredByID  referrerID
> 
> 1001    1              2
> 
> 1002    2              3
> 
> 1003    2              4
>
> 1004    5              7

From the above table structure i need to get the users whom i referred and the users whom are referred by their referrals.

For Example:

  • I am referredByID-1

  • I referred the ID – 2

  • Now the ID – 2 referred ID -3

  • And in the same case ID-2 referred ID – 4

Now my output needs to be look like:

Referrals Done By Me:

id – 2

id – 3

id – 4

How can this be done using MYSQL.

Any help will be appreciated.. Thanks in advance…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T06:58:57+00:00Added an answer on May 19, 2026 at 6:58 am

    I think I got everything the right way round but your naming conventions confused me so you’d better check everything.

    If I call the following stored procedure:

    call referrals_hier(1);
    

    I get the following results:

    +--------------+------------+-------+
    | referredByID | referrerID | depth |
    +--------------+------------+-------+
    |            1 |          2 |     0 |
    |            2 |          3 |     1 |
    |            2 |          4 |     1 |
    +--------------+------------+-------+
    3 rows in set (0.00 sec)
    

    full script here: http://pastie.org/1466596

    Stored procedure

    drop table if exists referrals;
    create table referrals
    (
    id smallint unsigned not null primary key,
    referrerID smallint unsigned not null,
    referredByID smallint unsigned null,
    key (referredByID)
    )
    engine = innodb;
    
    insert into referrals (id, referredByID, referrerID) values
     (1001,1,2),(1002,2,3),(1003,2,4),(1004,5,7);
    
    drop procedure if exists referrals_hier;
    
    delimiter #
    
    create procedure referrals_hier
    (
    in p_refID smallint unsigned
    )
    begin
    
    declare v_done tinyint unsigned default(0);
    declare v_dpth smallint unsigned default(0);
    
    create temporary table hier(
     referredByID smallint unsigned, 
     referrerID smallint unsigned, 
     depth smallint unsigned
    )engine = memory;
    
    insert into hier select referredByID, referrerID, v_dpth from referrals where referredByID = p_refID;
    
    /* http://dev.mysql.com/doc/refman/5.0/en/temporary-table-problems.html */
    
    create temporary table tmp engine=memory select * from hier;
    
    while not v_done do
    
        if exists( select 1 from referrals e inner join hier on e.referredByID = hier.referrerID and hier.depth = v_dpth) then
    
            insert into hier select e.referredByID, e.referrerID, v_dpth + 1 
                from referrals e inner join tmp on e.referredByID = tmp.referrerID and tmp.depth = v_dpth;
    
            set v_dpth = v_dpth + 1;            
    
            truncate table tmp;
            insert into tmp select * from hier where depth = v_dpth;
    
        else
            set v_done = 1;
        end if;
    
    end while;
    
    select * from hier order by depth;
    
    drop temporary table if exists hier;
    drop temporary table if exists tmp;
    
    end #
    
    delimiter ;
    
    -- call this sproc from your php
    
    call referrals_hier(1);
    

    Hope this helps 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want make a simple referral system. Basically referral URL structure something like this.
Having trouble understanding how to filter an images table by tag information in a
Having just moved from textmate to vim I'm curious. To be able to navigate
Having read this past question for git, I would like to ask if there
Having a headache with IE. I have an image (24x24) which I'd like to
Having properly configured a Development server and a Production server, I would like to
I am trying to parse a Referral URL from Google but there are no
Having created an array of values using a MySQL query, I need to check
Having specified table cell width <td style=width:100px> when content width is more than 100px
I have a table called users which looks like: -id -email -login -admin -coins

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.