Answer the question
In order to leave comments, you need to log in
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.
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');
Answer the question
In order to leave comments, you need to log in
And why sign up for the inviter?
Add a field to the users table indicating the inviter.
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
SELECT `name`
FROM `mp_tree`
WHERE :path LIKE CONCAT(`path`, '.%')
ORDER BY `path` DESC
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question