L
L
Lev Rozanov2016-01-12 21:57:44
PHP
Lev Rozanov, 2016-01-12 21:57:44

How to record referrals in the database by the counter?

Good day!
In general, there is a database structure "Materialized path (Materialized Path)" for the REFERRAL system.
55c36b37b77a5d3c8e23b66fd53cd807.gif
Entries in the database go as follows:

CREATE TABLE mp_tree (
    `id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `name` VARCHAR(50) NOT NULL,
    `path` VARCHAR(100) NOT NULL
) TYPE=InnoDB DEFAULT CHARSET=utf8;

CREATE INDEX mpp_idx ON mp_tree (`path`);

INSERT INTO mp_tree VALUES
    (1, 'FOOD', '1'),
    (2, 'VEGETABLE', '1.1'),
    (3, 'POTATO', '1.1.1'),
    (4, 'TOMATO', '1.1.2'),
    (5, 'FRUIT', '1.2'),
    (6, 'APPLE', '1.2.1'),
    (7, 'BANANA', '1.2.2');

Question: what SQL query or PHP function can write to the database if registration is used. Those. the person came to the site, filled out the form, indicated the login of the one who invited (or by the link) and after submitting the form signed up in the database under the inviter?
Help, good people. I've been looking for 3 weeks. Write like or poke your nose.
Thanks in advance.
I wish you success!
PS Material from Habry is used

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Peter, 2016-01-12
@petermzg

And why sign up for the inviter?
Add a field to the users table indicating the inviter.

R
Rsa97, 2016-01-12
@Rsa97

Parents by user id:

SELECT `t2`.`name`
    FROM `mp_tree` AS `t1`
    JOIN `mp_tree` AS `t2`
        ON `t1`.`path` LIKE CONCAT(`t2`.`path`, '.%')
    WHERE `t1`.`id` = :id
    ORDER BY `t2`.`path` DESC

Parents by user path:
SELECT `name`
    FROM `mp_tree`
    WHERE :path LIKE CONCAT(`path`, '.%')
    ORDER BY `path` DESC

The index will not be used because `path` is used inside the function.
I would do it on a Nested Set , where the insert will take longer, but the fetch is faster.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question